Node.js 节点';s watchFile()问题

Node.js 节点';s watchFile()问题,node.js,Node.js,问题1。我有以下代码。但当我更改并保存config1.json时,它会输出初始配置:,而不会输出新配置。我做错了什么 var fs = require("fs"); console.log("Started"); var config = JSON.parse(fs.readFileSync("./files/config1.json")); console.log("Initial config: ", config); fs.watchFile("./files/config1.json"

问题1。我有以下代码。但当我更改并保存config1.json时,它会输出初始配置:,而不会输出新配置。我做错了什么

var fs = require("fs");
console.log("Started");
var config = JSON.parse(fs.readFileSync("./files/config1.json"));
console.log("Initial config: ", config);

fs.watchFile("./files/config1.json", function(current, previous){
    console.log("Config changed");
    config = JSON.parse(fs.readFileSync("./files/config1.json"));
    console.log("New config file: ", config);
});
上述输出如下

node watch.js
Started
Initial config:  { username: 'ollie',
  api_key: 'parsley',
  name: 'Ollie Parsley',
  version: 112 }
问题2。由于它在候机楼里观看,我无法离开或退出它。如何从终端的上述代码中退出


提前谢谢

Q1。我发现,如果我使用curr和prev,它会起作用

var fs = require("fs");
console.log("Started");
var config = JSON.parse(fs.readFileSync("./files/config1.json"));
console.log("Initial config: ", config);

fs.watchFile("./files/config1.json", function(curr, prev){
    console.log("Config changed");
    config = JSON.parse(fs.readFileSync("./files/config1.json"));
    console.log("New config file: ", config);
});
问题2。Control+c将停止运行的代码