Closed azl397985856 closed 4 years ago
How Can I Know the file is changed
. Luckily, NodeJS provide fs.watch
API which referred in the problem description.
How Can I restart with the latest content. The solution is simply, We can use child_process.spawn
. Keep Only One worker process by Killing the subprocess when we just going to spawn a new one, AKA Singleton . 😄
nodemon.js
, Then Just Run:
node nodemon.js
nodemon.js:
const { spawn } = require("child_process");
const { watchFile } = require("fs");
const filename = process.argv[1];
let subprocess = null;
console.log("file-changed");
function restart(filename) {
const subprocess = spawn("node", [filename], {
detached: true
});
subprocess.stdout.on("data", data => {
console.log(`stdout1: ${data}`);
});
return subprocess;
}
watchFile(filename, () => {
if (subprocess !== null) {
subprocess.kill("SIGKILL");
}
subprocess = restart(filename);
});
nodemon.js
file , then observe the output "file changed"
Honestly the very first "file change" is kind of misunderstanding, If you also notice that, forgive me anyway.
热启动指的是修改文件,APP能够重新加载修改后的文件,然后重新执行。 像nodemon,pm2都有这样的功能。
让你实现一个功能,JS文件被修改之后,能够重新运行被修改的JS文件
提示:
nodejs提供一些api,让你对文件以及文件夹进行监听,eg:
扩展: 如果要优化速度怎么做呢?(比如增量更新或者按需更新)