在開發環境下,往往需要一個工具來自動重啟項目工程,之前接觸過 python 的 supervisor,現在寫 node 的時候發現 supervisior 在很多地方都有他的身影,node 也有一個 npm 模塊 supervisior 也是用來監控進程的,不過除了 supervisior 外,還有很多其他的工具,從 github 的評分上看,比較熱門的有 forever,nodemon,node-dev,具體這些工具的區別可以參考這篇文章 Comparison: Tools to Automate Restarting Node.js Server After Code Changes ,個人覺得在開發環境還是用 nodemon,因為配置比較方便,文檔也很清晰。所以這里先主要講 nodemon。
nodemon 的安裝:
npm install -g nodemon
安裝完 nodemon 后,就可以用 nodemon 來代替 node 來啟動應用:
nodemon [your node app]
(相當於 node [your node app])
如果沒有在應用中指定端口,可以在命令中指定:
nodemon ./server.js localhost 8080
可以運行 debug 模式:
nodemon --debug ./server.js 80
查看幫助,幫助里面有很多選項都是一目了然:
nodemon -h
或者 nodemon -help
nodemon 比較流行的原因之一就是它的可配置性比較高,下面是官網給出的配置文件 nodemon.json 的例子,加上我自己了解到的有用的一些配置,開發環境建議可以把每個參數都寫上備用,生產環境就把沒有必要的參數去掉,有些字段是可以在命令行模式以參數形式給出的,可以通過 -h 查看,下面逐個解釋:
{
"restartable": "rs",
"ignore": [
".git",
"node_modules/**/node_modules"
],
"verbose": true,
"execMap": {
"": "node"
"js": "node --harmony"
},
"events": {
"restart": "osascript -e 'display notification \"App restarted due to:\n'$FILENAME'\" with title \"nodemon\"'"
},
"watch": [
"test/fixtures/",
"test/samples/"
],
"env": {
"NODE_ENV": "development",
"PORT": "3000"
},
"ext": "js json",
"legacy-watch": false
}
View Code <span><span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;"><strong>restartable</strong>
:重啟的命令,默認是 rs ,可以改成你自己喜歡的字符串。當用 nodemon 啟動應用時,可以直接鍵入 rs 直接重啟服務。除了字符串值外,還可以設置 false 值,這個值的意思是當 nodemon 影響了你自己的終端命令時,設置為 false 則不會在 nodemon 運行期間監聽 rs 的重啟命令。
ignore:忽略的文件后綴名或者文件夾,文件路徑的書寫用相對於 nodemon.json 所在位置的相對路徑,下同。nodemon 會默認忽略一些文件,默認忽略的文件有:.git, node_modules, bower_components, .sass-cache,如果這些文件想要加入監控,需要重寫默認忽略參數字段 ignoreRoot,比如加入:“ignoreRoot”: [".git", “bower_components”, “.sass-cache”],然后在 watch 中將 node_modules 文件路徑加入監控,那么 node_modules 內的文件也加入了監控了。
execMap:運行服務的后綴名和對應的運行命令,“js”: “node --harmony” 表示用 nodemon 代替 node --harmony 運行 js 后綴文件;"" 指 www 這些沒有后綴名的文件;默認的 defaults.js 配置文件會識別一些文件:py: ‘python’,rb: ‘ruby’。
events:這個字段表示 nodemon 運行到某些狀態時的一些觸發事件,總共有五個狀態:
start - 子進程(即監控的應用)啟動
crash - 子進程崩潰,不會觸發 exit
exit - 子進程完全退出,不是非正常的崩潰
restart - 子進程重啟
config:update - nodemon 的 config 文件改變
狀態后面可以帶標准輸入輸出語句,比如 mac 系統下設置: “start”: “echo ‘app start’”,那么啟動應用時會輸出 app start 信息,其他類似命令如 ls,ps 等等標准命令都可以在這里定義。除此之外,也可以寫js來監控,github 上有介紹: events.md ,不過我試過之后沒有成功,如果有懂的朋友,記得在評論不吝賜教。_
watch:監控的文件夾路徑或者文件路徑。
env:運行環境 development 是開發環境,production 是生產環境。port 是端口號。
ext:監控指定后綴名的文件,用空格間隔。默認監控的后綴文件:.js, .coffee, .litcoffee, .json。但是對於沒有文件后綴的文件,比如 www 文件,我暫時找不到怎么用 nodemon 去監控,就算在 watch 中包含了,nodemon 也會忽略掉。
注:關於監控以及忽略文件修改有個順序的問題,或者說優先級,首先 nodemon 會先讀取 watch 里面需要監控的文件或文件路徑,再從文件中選擇監控 ext 中指定的后綴名,最后去掉從 ignore 中指定的忽略文件或文件路徑。
legacy-watch:nodemon 使用 Chokidar 作為底層監控系統,但是如果監控失效,或者提示沒有需要監控的文件時,就需要使用輪詢模式(polling mode),即設置 legacy-watch 為 true,也可以在命令行中指定:
$ nodemon --legacy-watch
$ nodemon -L # 簡寫
下面貼出 nodemon 的默認配置文件 default.js:
1 // default options for config.options
2 module.exports = {
3 restartable: ‘rs’,
4 colours: true,
5 execMap: {
6 py: ‘python’,
7 rb: ‘ruby’,
8 // more can be added here such as ls: lsc - but please ensure it’s cross
9 // compatible with linux, mac and windows, or make the default.js
10 // dynamically append the .cmd
for node based utilities
11 },
12 ignoreRoot: [’.git’, ‘node_modules’, ‘bower_components’, ‘.sass-cache’],
13 watch: [’.’],
14 stdin: true,
15 runOnChangeOnly: false,
16 verbose: false,
17 // ‘stdout’ refers to the default behaviour of a required nodemon’s child,
18 // but also includes stderr. If this is false, data is still dispatched via
19 // nodemon.on(‘stdout/stderr’)
20 stdout: true,
21 };
View Code
有幾個比較少用到的配置字段:
1) colous :輸出信息顏色標示。
2) runOnChangeOnly :true 時運行 nodemon www 項目不會啟動,只保持對文件的監控,當監控的文件有修改並保存時才會啟動應用,其他沒有影響。默認是 false 即一開始就啟動應用並監控文件改動。
3) stdin,stdout :這個是關於標准輸入輸出的設置,上文提到 nodemon.json 文件中的 events 字段可以為狀態設置標准輸入輸出語句,如果這里設置了 false,標准輸入輸入語句就會實效。
github 上給出了一個 faq.js 解答了一些常見的問題,有的上文已經提到,還有一些比較常見的列舉如下:
1)當自己的應用啟動服務帶的參數和 nodemon 沖突時,可以利用下面的方法來解決沖突:
$ nodemon app.js – -L -opt2 -opt3
以 – 為分隔,nodemon 不會去讀取 – 后面的參數,而是傳給 app.js。
2)當應用因為某些原因奔潰時,nodemon 不會自動重啟,會輸出以下信息:
[nodemon] app crashed - waiting for file changes before starting…
這個時需要修改文件並保存后 nodemon 才會重啟應用,這在開發環境沒什么關系,但是如果想把 nodemon 放在線上時,我們往往希望 nodemon 能夠自動重啟崩潰的應用,這個時候就需要 forever 來輔助了,有一個 issue 專門講這個問題。使用 forever 來重啟 nodemon 時,在 nodemon 啟動時需要加個參數 --exitcrash:
nodemon www --exitcrash
這樣當應用崩潰后,nodemon 會自動中斷退出,forever 檢測到 nodemon 退出后就會重啟 nodemon,nodemon 又會重啟應用。其他就是 forever 的配置了,因為這里只講 nodemon,所以就不涉及 forever,到時候總結 forever 的時候再講,感興趣的看 這里 。
3)如果想通過 npm start 命令來啟動應用同時又想用 nodemon 來監控文件改動,可以修改 npm 的 package.js 文件中的 scripts.start:
1 “scripts”: {
2 “start”: “nodemon ./bin/www”
3 }
那么用 npm start 啟動后就是執行 nodemon ./bin/www。
參考文檔:
github remy/nodemon . README.md
github remy/nodemon . faq.md
github remy/nodemon . events.md
codeplex . nodemon
bubkoo . 在 Express 開發中使用 nodemon
Qihan Zhang . Comparison: Tools to Automate Restarting Node.js Server After Code Changes