您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页Node.js 应用程序自动重启

Node.js 应用程序自动重启

来源:二三四教育网

在调试 Node.js 应用程序的时候,只要修改了 js 文件,就需要先 Ctrl + C 停止运行,再重新运行,修改后的 js 文件才会生效。

有没有办法修改 js 文件并保存后,Node.js 应用程序可以自动重启呢?Node Supervisor 正是这样一个可以实现这种需求的 Node.js 模块。

一、Supervisor 安装

安装方法(全局安装):

npm install supervisor -g

在当前 js 文件目录执行:supervisor xxx.js,例:

supervisor server.js

此时,只要修改并保存了 js 文件后,Node.js 应用程序便会自动重启。

一、Supervisor 参数

Supervisor 还支持多种参数:

  • -w|–watch:要监控的文件夹或 js 文件,默认为‘.’。
  • -i|–ignore:忽略监控的文件夹或 js 文件。
  • -p|–poll-interval:监控文件变化的时间间隔(周期),默认为 Node.js 内置的时间。
  • -e|–extensions:要监控的文件扩展名,默认为‘node|js’。
  • -x|–exec:要执行的主应用程序,默认为‘node’。
  • –debug:开启 debug 模式(用 –debug flag 来启动 node)。
  • -q|–quiet:安静模式,不显示 debug 信息。

例:

supervisor -q server.js

不显示 debug 信息

server.js

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((request, response) => {
    response.statusCode = 200;
    response.setHeader('Content-Type', 'text/plain;charset=utf-8');
    response.end('http 模块。');
});
server.listen(port, hostname, () => {
    console.log(`服务器运行在 http://${hostname}:${port}`);
});

期待您的关注!

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务