在Express.js中的一些间隔后刷新variables

如何从外部json文件中以固定间隔从Express.js中获取值?

那么这是非常含糊的,但你可以做的是设置一个函数,定期执行并从JSON文件中获取值。

// Fetch values every 3 seconeds setInterval(function(){ var fs = require('fs'); var file = __dirname + '/test.json'; fs.readFile(file, 'utf8', function (err, data) { if (err) { console.log('Error: ' + err); return; } data = JSON.parse(data); // Now you have a JSON object from which you can extract values. // You can save those values somewhere or just log them to the console console.dir(data); }); }, 3000);