開發 Node.js 實現的 HTTP 應用時會發現,無論你修改了程式碼的哪一部份,都必須終止Node.js 再重新執行才會奏效。

這是因為 Node.js 只在第一次引用到某部份時才會去解析指令碼檔案,以後都會直接訪問記憶體,避免重複載入。這種設計雖然

有利於提高效能,卻不利於開發除錯,因為開發過程中總是希望修改後立即看到效果,而不是每次都要終止程序並重啟。

  supervisor 可以幫助你實現這個功能,它會監視你對程式碼的改動,並自動重啟 Node.js。

Node.js的編譯安裝方法見http://www.cnblogs.com/felixzh/p/5822354.html
             使用方法很簡單,首先使用 npm 安裝 supervisor:

[felix@MyTerminal ~]$ sudo npm install -g supervisor
/usr/local/bin/node-supervisor -> /usr/local/lib/node_modules/supervisor/lib/cli-wrapper.js
/usr/local/bin/supervisor -> /usr/local/lib/node_modules/supervisor/lib/cli-wrapper.js
/usr/local/lib
鈹斺攢鈹€ [email protected]

[felix@MyTerminal ~]$ cd /usr/local/bin
[felix@MyTerminal bin]$ ls
c++ gcc gcc-ranlib i686-pc-linux-gnu-g++ i686-pc-linux-gnu-gcc-ar node supervisor
cpp gcc-ar gcov i686-pc-linux-gnu-gcc i686-pc-linux-gnu-gcc-nm node-supervisor
g++ gcc-nm i686-pc-linux-gnu-c++ i686-pc-linux-gnu-gcc-4.8.5 i686-pc-linux-gnu-gcc-ranlib npm
[felix@MyTerminal bin]$

接下來,使用 supervisor 命令啟動 app.js:
[felix@MyTerminal bin]$ supervisor app.js
DEBUG: Running node-supervisor with
DEBUG: program 'app.js'
DEBUG: --watch '.'
DEBUG: --extensions 'node|js'
DEBUG: --exec 'node'
DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory '/home/byvoid/.' for changes.
HTTP server is listening at port 3000.
當代碼被改動時,執行的指令碼會被終止,然後重新啟動。在終端中顯示的結果如下:
DEBUG: crashing child
DEBUG: Starting child process with 'node app.js'
HTTP server is listening at port 3000.

supervisor 這個小工具可以解決開發中的除錯問題。