javascript - 如何将变量从服务器js文件传递到另一个文件

标签 javascript html node.js api express

我正在构建一个简单的天气网页。我使用 Node 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 从其他文件中进行请求。

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

最佳答案

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

关于javascript - 如何将变量从服务器js文件传递到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61001641/

相关文章:

javascript - 文本如何响应地在 div 中垂直居中对齐

javascript - 如何通过单击页面上的任意位置来关闭下拉菜单?

node.js - 如何用NodeJs/Electron抓取计算机名?

javascript - 使用 JavaScript 调整按钮样式呈现 CSS :active event useless

javascript - AngularJS $http.post 上的错误 access-control-allow-origin

python - 如何在 Tornado 中创建多个 websocket 聊天?

javascript - 如何检查函数是否在 Node 的工作线程中运行?

javascript - 我如何删除从cheerio返回的数据中的 "\n"

javascript - 如何制作一个可编辑的jquery函数

javascript - Ag Grid 多行单元格内容