0.Node.js開發的特點
在初上手Node.js開發的時候,我們要啓動一個服務,使用的是 node
命令:
node myapp
然而,node告訴我們,服務端的js代碼只有在node第一次引用,纔會重新加載;如果node已經加載了某個文件,即使我們對它進行了修改, node也不會重新加載這個文件。
那麼,在開發過程中,要如何才能像PHP一樣,修改某個文件後,直接刷新網頁就能看到效果呢?
方法有很多,比如使用 pm2
、forever
等來管理,比如使用今天要介紹的 supervisor
。
1.安裝
nodejs下載supervisor:
npm install -g supervisor
這裏需要特別說明的是, -g
意味着安裝到全局,所以Linux和Mac系統在安裝時,要加 sudo
:
sudo npm install -g supervisor
在這種安裝方式下,
supervisor
將被安裝到默認的全局文件夾中;如果你不希望這樣,可以修改全局路徑爲當前路徑[1]:
npm config set prefix <你的路徑>
2.基本使用
最常用、最快捷的方式,就是直接進入你的網站根目錄,執行:
supervisor myapp
這裏要說明的一點是,不論你的網站服務啓動文件在什麼位置,你必須在根目錄啓動它。
舉個例子,Express4.0中,啓動文件位於 ./bin/www
中,則我們啓動時,必須在 ./
中執行:
supervisor bin/www
而不能進入 bin
目錄執行: supervisor www
。這樣雖然有可能也能啓動,但這麼做相當於把 bin
目錄當作了服務的根目錄了,一旦有涉及到文件目錄的操作,一定會出錯的。
另外,執行完這個命令後,我們的網站服務就已經啓動了;不過,需要注意的是,這種方式啓動的服務,是默認監控所有文件、文件夾的變化的;一旦有變化,服務就會重啓(這個是node特性造成的,如果你對node的原理感興趣,可以閱讀這篇文章:《Node.js 包教不包會》)。
這樣就出現了一些問題:我們會將一些日誌文件存入某些文件夾,或者用戶會上傳附件到服務器;而這樣的操作都導致了服務器文件的變化,必然會引起node服務器的重啓。試想一下,如果每一次上傳都重啓一次,那用戶操作一旦頻繁起來,服務器啥都不用幹,每天重啓就行了。
所以說,supervisor
的這種工作方式,僅僅適用於調試階段;甚至於有一些調試環境都不適合(比如調試服務器在遠程,網絡狀態不是很好的情況下)。
那麼要如何解決呢?請往下看。
3.更多使用方法
我們在命令行中直接執行:
supervisor
會得到它的詳細使用方法:
Node Supervisor is used to restart programs when they crash.
It can also be used to restart programs when a *.js file changes.
Usage:
supervisor [options] <program>
supervisor [options] -- <program> [args ...]
Required:
<program>
The program to run.
Options:
-w|--watch <watchItems>
A comma-delimited list of folders or js files to watch for changes.
When a change to a js file occurs, reload the program
Default is '.'
-i|--ignore <ignoreItems>
A comma-delimited list of folders to ignore for changes.
No default
--ignore-symlinks
Enable symbolic links ignoring when looking for files to watch.
-p|--poll-interval <milliseconds>
How often to poll watched files for changes.
Defaults to Node default.
-e|--extensions <extensions>
Specific file extensions to watch in addition to defaults.
Used when --watch option includes folders
Default is 'node,js'
-x|--exec <executable>
The executable that runs the specified program.
Default is 'node'
--debug[=port]
Start node with --debug flag.
--debug-brk[=port]
Start node with --debug-brk[=port] flag.
--harmony
Start node with --harmony flag.
--harmony_default_parameters
Start node with --harmony_default_parameters flag.
-n|--no-restart-on error|exit
Don't automatically restart the supervised program if it ends.
Supervisor will wait for a change in the source files.
If "error", an exit code of 0 will still restart.
If "exit", no restart regardless of exit code.
If "success", no restart only if exit code is 0.
-t|--non-interactive
Disable interactive capacity.
With this option, supervisor won't listen to stdin.
-k|--instant-kill
use SIGKILL (-9) to terminate child instead of the more gentle SIGTERM.
--force-watch
Use fs.watch instead of fs.watchFile.
This may be useful if you see a high cpu load on a windows machine.
-h|--help|-?
Display these usage instructions.
-q|--quiet
Suppress DEBUG messages
-V|--verbose
Show extra DEBUG messages
Options available after start:
rs - restart process.
Useful for restarting supervisor eaven if no file has changed.
Examples:
supervisor myapp.js
supervisor myapp.coffee
supervisor -w scripts -e myext -x myrunner myapp
supervisor -- server.js -h host -p port
可以知道,如果想不監控某一些文件夾,可以使用 -i
參數。如:我們要忽略根目錄下的 private
文件夾,可以這樣啓動:
supervisor -i ./private myapp
如果要忽略多個文件夾,則用英文的逗號,
分隔:
supervisor -i ./private,./otherdir myapp
以上
參考資料:
[1] 資料來源:使用supervisor提高nodejs調試效率
作者:Mike的讀書季
鏈接:https://www.jianshu.com/p/6d84e5efe99d
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。