[轉] nodemon 基本配置與使用

在開發環境下,每每須要一個工具來自動重啓項目工程,以前接觸過 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。node

nodemon 的安裝:python

npm install -g nodemon

安裝完 nodemon 後,就能夠用 nodemon 來代替 node 來啓動應用:linux

nodemon [your node app](至關於 node [your node app]) 

若是沒有在應用中指定端口,能夠在命令中指定:nginx

nodemon ./server.js localhost 8080 

能夠運行 debug 模式:git

nodemon --debug ./server.js 80 

查看幫助,幫助裏面有不少選項都是一目瞭然:github

nodemon -h 或者 nodemon -help

nodemon 比較流行的緣由之一就是它的可配置性比較高,下面是官網給出的配置文件  nodemon.json  的例子,加上我本身瞭解到的有用的一些配置,開發環境建議能夠把每一個參數都寫上備用,生產環境就把沒有必要的參數去掉,有些字段是能夠在命令行模式以參數形式給出的,能夠經過 -h 查看,下面逐個解釋:npm

 1 {
 2 "restartable": "rs", 3 "ignore": [ 4 ".git", 5 "node_modules/**/node_modules" 6 ], 7 "verbose": true, 8 "execMap": { 9 "": "node" 10 "js": "node --harmony" 11 }, 12 "events": { 13 "restart": "osascript -e 'display notification \"App restarted due to:\n'$FILENAME'\" with title \"nodemon\"'" 14 }, 15 "watch": [ 16 "test/fixtures/", 17 "test/samples/" 18 ], 19 "env": { 20 "NODE_ENV": "development", 21 "PORT": "3000" 22 }, 23 "ext": "js json", 24 "legacy-watch": false 25 } 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 的重啓命令。</span></span>json

ignore:忽略的文件後綴名或者文件夾,文件路徑的書寫用相對於 nodemon.json 所在位置的相對路徑,下同。nodemon 會默認忽略一些文件,默認忽略的文件有:.git, node_modules, bower_components, .sass-cache,若是這些文件想要加入監控,須要重寫默認忽略參數字段 ignoreRoot,好比加入:"ignoreRoot": [".git", "bower_components", ".sass-cache"],而後在 watch 中將 node_modules 文件路徑加入監控,那麼 node_modules 內的文件也加入了監控了。vim

verbose:true 表示輸出詳細啓動與重啓信息,以下圖:windows

false 表示不輸出這些運行信息,以下圖:

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 # 簡寫

<span><span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;">下面貼出 nodemon 的默認配置文件&nbsp;</span><a style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;" href="https://github.com/remy/nodemon/blob/master/lib/config/defaults.js">default.js</a><span style="font-family: verdana, Arial, Helvetica, sans-serif; line-height: 1.5;">:</span></span>

 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

水平有限,錯誤歡迎指正。原創博文,轉載請註明出處。

相關文章
相關標籤/搜索