首页 > 解决方案 > 将数据从 api 传递到 html 文件

问题描述

所以我使用openweather api来获取天气数据,我想将数据发送到一个html文件或者一个嵌入html的js文件中,但我不知道我该怎么做。这是我到目前为止得到的代码

app.get("/",function(req,res){
  const url="https://api.openweathermap.org/data/2.5/weather?q=Toronto &appid=apikey&units=metric";
 https.get(url,function(response){
    response.on("data",function(data){
    const weatherData=JSON.parse(data);
    var temp=weatherData.main.temp;
    var describtion=weatherData.weather[0].description;
    var icon=weatherData.weather[0].icon;
    var imgURL="http://openweathermap.org/img/wn/"+icon+"@2x.png";


    res.write("<p>The weather is "+ describtion + "</p>");
    res.write("<h1>The temperature in " +"Toronto is "+ temp + " celcius</h1>");
    res.write("<img src="+imgURL+">");


    });
  res.sendFile(__dirname+"/index.html");
});
});

我想传递临时、描述和 img url,并在用户发送 get 请求时显示在 html 中,默认情况下它显示多伦多的天气而没有发布请求。谢谢!!!

标签: apihttps

解决方案


推荐阅读