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