node.js watchFile()没有看

时间:2014-01-24 20:29:58

标签: javascript json node.js

我写了这个小代码来观看 config.json 文件,但是当我运行文件时

node watch.js

并更改config.json文件值中的值没有任何更改没有更新的文件显示在cmd上,cmd有点冻结。

var fs = require("fs");

console.log("Started");

var config = JSON.parse(fs.readFileSync("./files/config.json"));

console.log("Initial config: ", config);  

fs.watchFile(".files/config.json", function(current, previous){  

    cconsole.log("Config Changed");  
    config = JSON.parse(fs.readFileSync("./files/config.json"));  
    console.log("New Config file: ", config);

});

1 个答案:

答案 0 :(得分:2)

你在fs.watchFile中有一个拼写错误,你告诉它要看“.files”,它应该是“./files”。

另外,作为一种快捷方式,如果您使用的是JSON,则可以执行以下操作:

var config = require("./files/config.json");

并且您的JSON将被解析为存储在config

中的对象