發表文章

[Node.js] require 的機制

最近在面試的時候, 寫了一些基本的題目, 但是卻有些地方是我自以為清楚, 但是不清楚的地方, 那就是這次要寫的 require! 其實一般人使用應該都會知道 require 完後, Node.js 會把 require 的物件, 會快取在記憶體內, 但是由於我真的太久沒寫, 所以在沒有理解的情況下, 也會忘記是不是真的每次 require 後就會存在記憶體中, 所以這次來分析一下原始碼 https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js // Loads a module at the given file path. Returns that module's // `exports` property. Module.prototype.require = function(id) { validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string'); } requireDepth++; try { return Module._load(id, this, /* isMain */ false); } finally { requireDepth--; } }; 從上面就可以看出, 關鍵是在 Module._load 那我們再看一下 Module._load 的程式碼 // Check the cache for the requested file. // 1. 假如一個 module 已經存在快取中, 直接返回其 exports 的物件 // 2. 假如 module 是原生系統內部的 module (ex: file, path), // 呼叫 NativeModule.prototype.compileForPublicLoader() 並且返回其 exports...

[Laravel] 讓你的 Helper 更有彈性 (使用 facades)

在 Laravel 中,有些常使用的 class 像是: Cache 在進行單元測試的時候,因為 Cache 本身繼承 Facade 所以可以很容易地在單元測試中 mock Cache ex: Cache::shouldReceive('get') ->once() ->with('key') ->andReturn('value'); 如果像是我們自己實作的 Helper,也想要在靜態類別也可以有 mockery 的 shouldReceive 該如何實作? 官方文件有提到可以使用 Facades 舉我們自己建立的 HttpHelper 為例 1. 建立 HttpHelper class <?php namespace App\Helpers; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Illuminate\Http\Request; class HttpHelper { // ... 功能略 } 2. 建立 Provider,並且將 HttpHelper 綁定到這個 HttpServiceProvider php artisan make:provider HttpServiceProvider <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\App; class HttpHelperServiceProvider extends ServiceProvider { public function boot() { } /** * Register the application services. * * @return void */ public function register() { App::bind('Ht...

[PHP] Lumen 沒有 php artisan serve 但又想要有類似的功能怎辦?

來源: https://stackoverflow.com/questions/47385324/laravel-controller-error-when-using-request-in-function 解法: php -S 127.0.0.1:8000 -t ./public

[PHP] 在 MacOS 上安裝 php mongodb driver

圖片
因為用mac透過 pecl 去做一定會狂出問題 所以自己編譯會比較方便 $ git clone https://github.com/mongodb/mongo-php-driver.git $ cd mongo-php-driver $ git submodule update --init $ phpize $ ./configure $ make all $ sudo make install 假如出現 Security/Security.h file not found 可以參考這篇 https://stackoverflow.com/questions/53531008/pecl-install-mongodb-on-mac-fatal-error-security-security-h-file-not-found cd mongo-php-driver/include ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Versions/A/Headers/ Security ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/A/Headers/ CoreFoundation brew install pcre make 最後記得編輯 php.ini vi /etc/php.ini 在裡面新增 extension=mongodb.so 可以先用 php -i | grep extension_dir 得知extension directory在哪 大概像是這樣: 之後進去該目錄,檢查 mongodb.so 是否在裡面 然後用 php -m | grep mongo 檢查是不是有進到設定檔中 這篇也有參考價值 https://www.jianshu.com/p/1fd7c1927ae5 brew install autoconf 如果出現找不...
ifconfig eth0:0 192.168.2.4

[Linux] CentOS python2.6 升級 2.7 但不影響yum, 安裝pip

Ref: https://snippetinfo.net/media/1955 https://www.itread01.com/articles/1498939033.html yum groupinstall "Development tools" yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel cd /opt wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz tar xf Python-2.7.6.tar.xz cd Python-2.7.6 ./configure --prefix=/usr/local make && make altinstall 其實主要需要解決的是系統 Python soft link 指向 Python2.7 版本後 因為yum是不兼容 Python 2.7的 所以yum不能正常工作 我們需要指定 yum 的Python版本 vi /usr/bin/yum 將文件頭部的 !/usr/bin/python 改成 !/usr/bin/python2.6 (此處為系統內原本python2.6的位置) 然後把新裝好的 python 用 soft link 指回去 /usr/bin/python 就可以囉! ln -s /usr/bin/python2.7 /usr/local/bin/python 另外不想用yum install python-pip的話 可以用: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py # pip安裝好之後可以使用 pip -V 看版本 # 要安裝套件就直接 pip install 套件 pip install flask pip install redis pip install scrapy

[Nginx] 使用轉址做負載平衡

Ref: http://blog.51cto.com/smileyouth/1623766 https://serverfault.com/questions/597671/load-balancing-in-nginx-with-redirect-rather-than-proxy https://stackoverflow.com/questions/46515842/nginx-proxy-pass-same-protocol-http-https 有個需求是這樣的 現在前端使用某一個 url, 假設是 http://example.com/test 然後希望前端載入這個網址的時候, 可以讓這個 url 隨機導向N個不同的網域, 假設是 a.example.com, b.example.com, c.example.com 但是後面路徑要是一樣的 也就是 http://example.com/test?t=1 -> http://c.example.com/test?t=1 這樣該怎麼做呢? 先假設 nginx 路徑是安裝在 /usr/local/nginx 先建立一個 /usr/local/nginx_module的目錄 安裝這個 module 要先下載另一個 ngx_devel_kit 先到  https://github.com/simplresty/ngx_devel_kit/tags 找個版本下載 cd ~ && wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz tar zxfv v0.3.1rc1.tar.gz #把解壓縮出來的目錄去掉版本號 mv ngx_devel_kit-0.3.1rc1 ngx_devel_kit && mv ngx_devel_kit /usr/local/nginx_module/ 然後接著下載  set-misc  這個 module 先到  https://github.com/openresty/set-misc-nginx-module/tags 找個版本下載 c...