【问题标题】:how to pass variable from server js file to another如何将变量从服务器js文件传递到另一个
【发布时间】:2020-04-02 21:41:45
【问题描述】:

我正在构建一个简单的天气网页。我使用节点 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 文件中

【问题讨论】:

    标签: javascript html node.js api express


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-25
      • 2014-07-02
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多