##0.Node.js开发的特点

在初上手Node.js开发的时候,我们要启动一个服务,使用的是 node 命令:
node myapp
然而,node告诉我们,服务端的js代码只有在node第一次引用,才会重新加载;如果node已经加载了某个文件,即使我们对它进行了修改, node也不会重新加载这个文件。
那么,在开发过程中,要如何才能像PHP一样,修改某个文件后,直接刷新网页就能看到效果呢?
方法有很多,比如使用 pm2、forever 等来管理,比如使用今天要介绍的 supervisor。

##1.安装

废话不多说,直接上干货:
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服务器的重启。试想一下,如果每一次上传都重启一次,那用户操作一旦频繁起来,服务器啥都不用干,每天重启就行了(我的网站就出现过这样的问题,参看:2017.03.25_ajax更新数据时报错net::ERR_CONNECTION_RESET)。
所以说,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]
supervisor [options] – [args …]

Required:

The program to run.

Options:
-w|–watch
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
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
How often to poll watched files for changes.
Defaults to Node default.

-e|–extensions
Specific file extensions to watch in addition to defaults.
Used when –watch option includes folders
Default is ‘node,js’

-x|–exec
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调试效率