Node.js

Node.js 应用程序自动重启

2019-01-19  本文已影响0人  e20a12f8855d

在调试 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 还支持多种参数:

例:

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}`);
});

期待您的关注!

上一篇 下一篇

猜你喜欢

热点阅读