0

我正在建立一个简单的天气网页。我使用节点 js 服务器文件从开放天气 api 获取天气信息

问题是,一旦我获得温度等天气信息,然后将其设置在一个变量上,我就无法将其传递给我的其他 javascript 文件,以便能够在我的新 html 文件上显示它。我想将weatherState 变量传递给另一个js 文件,以便将它放在我的weather.html 中。这是代码

app.post("/", function(req, res) {
    const url = "https://api.openweathermap.org/data/2.5/weather?q=london&appid=****&units=metric

  https.get(url, function(response) {
    response.on("data", function(data) {
      const weatherData = JSON.parse(data);
      var weatherState = "The temperature in london  is: " + weatherData.main.temp + " degrees
      res.sendFile(__dirname + "/weather.html");

    });
  });
});

我试过做 module.export 的事情,但浏览器不会让我使用 node.js 从其他文件中要求。

我只是想让weatherState变量以某种方式显示在我的weather.html文件中

4

1 回答 1

1

尝试使用节点 js 的 Ejs 模板。在此处找到链接 https://scotch.io/tutorials/use-ejs-to-template-your-node-application

于 2020-04-02T21:50:40.557 回答