【发布时间】:2021-06-27 11:12:27
【问题描述】:
我想刷新 HTTP node.js 服务器中的浏览器页面。我试过nodemon,但它重新加载了服务器而不是浏览器。你能告诉我如何每 2 秒刷新一次浏览器页面吗?这是我的服务器代码,谢谢!
var http = require('http');
var port = 5000;
function lightSensor() {
var data = Math.random().toFixed(2)
console.log("Light sensor: " + data);
return data;
}
http.createServer(function(req,res){
res.writeHeader(200, {'Content-Type': 'application/json'});
res.write('{"Light sensor" : ' + lightSensor() + '}');
res.write('
res.end();
}).listen(port);
console.log('Server listening on: http://localhost:' + port);
process.on('SIGINT', function () {
process.exit();
});
【问题讨论】:
标签: javascript node.js http