【发布时间】:2020-12-15 04:26:50
【问题描述】:
我有一个简单的 node.js 应用程序,它使用 systeminformation 包检查当前树莓派温度,并检查 public ip,因为我使用动态 dns 公开我的 pi。它通过快速获取请求显示在浏览器中。我还使用 PM2 进行流程管理并保持 app.js 始终运行。
每次有对“/”的获取请求时,我想将时间戳、IP 和温度记录到 log.txt。下面的代码几乎可以解决问题。它在浏览器中显示温度和 IP,并且在将正确的时间戳和 IP 记录到“log.txt”时,温度(存储在变量“temp”中)始终显示为“未定义”。
const si = require('systeminformation');
const pip4 = require('public-ip');
const express = require('express');
const app = express();
const port = 3389;
const fs = require('fs');
let temp;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
app.get("/", (req, res, next) => {
//get cpu temperature and store it in ${temp}
si.cpuTemperature()
.then(data => {temp = data; })
.catch(error => console.error(error));
//get public IP and store it in ${ip}
(async () => {
let ip = (await pip4.v4());
const text = `${Date()} from IP: ${ip} and core temp: ${temp}` + '\r\n';
//append timestamp and IP info to log.txt
fs.appendFile('./log/log.txt', text, (err) => {
if (err) throw err;
console.log('Successfully logged request.');
});
//display cpu temperature and IP in the browser
setTimeout(() => {
res.json({
temp,
ip
});
}, 250);
})();
});
http 响应起作用并显示温度和 IP:
{"temp":{"main":37.485,"cores":[],"max":37.485},"ip":"x.x.x.x"}
但 log.txt 中记录的临时条目始终未定义:
Wed Aug 26 2020 13:07:30 GMT+0200 (Central European Summer Time) from IP: x.x.x.x and core temp: undefined
我知道这是由于一个未解决的承诺,但我似乎无法找到正确解决它的方法...
【问题讨论】:
标签: node.js express async-await raspberry-pi pm2