当前位置:首页 > Node.js自动启动更新

Node.js自动启动更新

发布于 2018-04-13 阅读 2384 次 NodeJs

每次修改了代码,nodejs都需要手动重启,不然不会更新。
supervisor 可以帮助你实现这个功能,它会监视你对代码的改动,并自动重启 Node.js。 使用方法很简单,首先使用 npm 安装 supervisor:

  1. $ npm install -g supervisor

如果你使用的是 Linux 或 Mac,直接键入上面的命令很可能会有权限错误。原因是 npm 需要把 supervisor 安装到系统目录,需要管理员授权,可以使用 sudo npm install -g supervisor 命令来安装。
接下来,使用 supervisor 命令启动 app.js: $

  1. supervisor app.js

DEBUG: Running node-supervisor with
DEBUG:
DEBUG:
DEBUG:
DEBUG:
program ‘app.js’
—watch ‘.’
—extensions ‘node|js’
—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 这个小工具可以解决开发中的调试问题。

参考:nodejs开发指南