Node.js event on json file change -
i'm beginner in node.js , i'm having trouble watch change of json file.
i'm using json file external url (let's http://www.example.com/data.json) , want check each second if json file changed , changed.
i have managed work think there better ways this.
var json; /** * get_json() json file , callback it's content */ function get_json(callback){ request({ url: url, json: true }, function (error, response, body){ if (!error && response.statuscode == 200) callback(body); }); } /** * there first initiate json variable compare */ get_json(function(data){ json = data; }); /** * loop compare stored json , new 1 each second */ setinterval(function(){ get_json(function(data){ (var prop in data) { if (data[prop].title != json[prop].title) { //do stuff data[prop] } } json = data; }); }, 1000); and json :
{"book":{"title":"hello"},"movie":{"title":"world"}} i learned events , streams i'd use tried far didn't worked.
thanks !
Comments
Post a Comment