Q1. I have the following code. But when I change and save config1.json, it outputs the Initial config: , does not output the new config. What am I doing wrong here?
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);
});
The above outputs the following.
node watch.js
Started
Initial config: { username: 'ollie',
api_key: 'parsley',
name: 'Ollie Parsley',
version: 112 }
Q2. Since it is watching in the terminal, I am not able to get out or quit it. How can I quit it from the above code in the terminal?
Thanks in advance.
Q1. I found that if I use curr and prev, it works.
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);
});
Q2. Control+c will stop the running code.