如果监听的文件发生变化,会自动重启服务。
启动入口
index.js
const {fork} = require('child_process');
const fs = require('fs');
let childPid = {
pid: null,
isAlive: true
};
/**
* 启动子进程
*/
function startServerProcess() {
//启动子进程并监听子进程发来的消息
let childProcess = fork('server.js');
//保存子进程的pid
childPid = {
pid: childProcess.pid,
isAlive: true
};
}
let fileName = './demo.txt';
//从命令行参数中获取watch文件名
if(process.argv.length === 4 && process.argv[2] === '--watch') {
fileName = './'+process.argv[3];
}
startServerProcess();
fs.watch(fileName,function () {
//因为windows下每次改变文件都会触发两次watch
//必须要判断进程是存活状态,否则kill会报错
if(childPid.isAlive) {
//杀死子进程
process.kill(childPid.pid);
//状态设置为死亡
childPid.isAlive = false;
//重启一个新的子进程
startServerProcess();
}
});
server.js
const express = require('express');
const fs = require('fs');
var app = express();
app.get('/',(req,res)=>{
let buffer = fs.readFileSync('./demo.txt');
res.send(buffer.toString());
});
app.listen(3000,()=>{
console.log('express init '+3000);
});