最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在由 node.js 创建的网络服务器上更新<div>

网站源码admin28浏览0评论

如何在由 node.js 创建的网络服务器上更新<div>

如何在由 node.js 创建的网络服务器上更新<div>

我用 Nodejs 创建了一个网络服务器,我正在将 CPU 温度写入控制台,但我想更新浏览器以显示温度。这是怎么做到的?我想不通……对不起,这可能是一个基本问题,但我确实有基本知识……

这是我在 Node.js 中运行的脚本。一切都按预期工作。我每 5 秒获取一次 CPU 温度,但如何更新 html 中的温度??

var http = require('http');
const { measureCPU } = require('rpi_measure_temp')
var PAGE = "<!DOCTYPE html><body><div id='cpu'>CPU</div></body></html>";
cpu_interval = setInterval(cpu, 5000);

//create a server object:
http.createServer(function (req, res) {
  res.write(PAGE);
  res.end();
}).listen(8080);

//measure CPU temperature
function cpu() {
  measureCPU()
  .then(result => {
    const decimalValue = result.celsius.toFixed(1)
    console.log(decimalValue);
    //how do I write the CPU temperature to the browser ???
    //document.getElementById("cpu").innerHTML = decimalValue;
   })
  .catch(error => {
    console.log(error) })
}
回答如下:
发布评论

评论列表(0)

  1. 暂无评论