Nodejs 和 Express:正确解决承诺

Nodejs & Express: properly resolve a promise

我有一个简单的 node.js 应用程序,它使用 systeminformation package and also checks the public ip 检查当前 raspberry pi 温度,因为我使用动态 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

我知道这是由于未解决的承诺,但我似乎无法找到正确解决它的方法...

使您的处理程序完全async;不要为混合和匹配而烦恼 .then():

const si = require("systeminformation");
const pip4 = require("public-ip");
const express = require("express");
const app = express();
const port = 3389;
const fs = require("fs");

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

app.get("/", async (req, res) => {
  const temp = await si.cpuTemperature();
  const ip = await pip4.v4();
  const text = `${Date()} from IP: ${ip} and core temp: ${temp}\r\n`;
  fs.appendFile("./log/log.txt", text, (err) => {
    if (err) console.error(`Logging: ${err}`);
  });
  res.json({ temp, ip });
});

您可以通过并行查询 IP 和 CPU 温度来加快速度:

app.get("/", async (req, res) => {
  const [temp, ip] = await Promise.all([si.cpuTemperature(), pip4.v4()]);
  const text = `${Date()} from IP: ${ip} and core temp: ${temp}\r\n`;
  fs.appendFile('./log/log.txt', text, (err) => {
    if (err) console.error(`Logging: ${err}`);
  });
  res.json({temp, ip});
});

不过,这两个都缺少错误处理。

编辑:另一种变体也记录为 JSON,以正确表达(呵呵)所有对象:

app.get("/", async (req, res) => {
  const [temp, ip] = await Promise.all([si.cpuTemperature(), pip4.v4()]);
  const logData = JSON.stringify({date: Date(), ip, temp}) + '\r\n';
  fs.appendFile('./log/log.txt', logData, (err) => {
    if (err) console.error(`Logging: ${err}`);
  });
  res.json({temp, ip});
});