返回

javascript-如何在 express.js 中设置使用 response.send () 发送的数据的样式

发布时间:2022-06-07 13:43:44 465
# 前端

我想设置我在 app.post() 方法中发回的数据的样式。我怎样才能做到这一点?

这是 app.post() 代码:

app.post("/",function(req,res){

  const query = req.body.cityName;

  const cityName = query.charAt(0).toUpperCase() + query.slice(1);

  const apiKey = "fc2f5f2ba2fe9d109d021ef0f57ef03d";

  const unit ="metric";

  const url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=" + apiKey + "&units=" + unit;

  https.get(url,function(response){

    console.log(response.statusCode);

    response.on('data', function(data){

      const weatherData = JSON.parse(data);

      const temp = weatherData.main.temp;

      const weatherDescription = weatherData.weather[0].description;

      const icon = weatherData.weather[0].icon;

      const imageURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png";

      res.write("<h1>The temperature in " + cityName + " is " + temp + " degrees Celcius</h1>");

      res.write("<h3>The weather is currently " + weatherDescription + "</h3>");

      res.write("<img src="+ imageURL +">");

      res.send();

    })

  });

})

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像