【发布时间】:2020-08-28 08:18:07
【问题描述】:
我正在尝试从外部服务器检索天气 API,当我在控制台记录天气 API 的特定数据时,它也会显示在我的命令提示符上。 但是当我使用 get 方法在浏览器上显示该数据时,我只能发送字符串数据,如“描述”:中雨,而不是数字数据,如“温度”:27 它会使应用程序崩溃。
节点js代码:
//jshint esversion:6
const express = require("express");
const app = express();
const https = require("https");
app.get("/", function(req, res) {
const url = "https://api.openweathermap.org/data/2.5/weather?q=mumbai&appid=d88391210768983e6be06cdd76bdcde3&units=metric";
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 description= weatherData.weather[0].description;
console.log(temp);
console.log(description);
res.send(temp);
});
});
});
app.listen(3000, function() {
console.log("Server is running on port: 3000");
});
【问题讨论】: