📅  最后修改于: 2022-03-11 14:48:31.591000             🧑  作者: Mango
const express = require("express");
const https = require("https");
const app = express();
const url =
"https://api.openweathermap.org/data/2.5/weather?q=London,uk&units=metric&appid=0333cb6bfed722ca09f1062ec1ea9ca1";
app.get("/", (req, res) => {
https.get(url, response => {
response.on("data", data => {
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const { description, icon } = weatherData.weather[0];
const imageURL = `http://openweathermap.org/img/wn/${icon}@2x.png`;
res.set("Content-Type", "text/html");
//OR
res.setHeader("Content-Type", "text/html");
res.send(`
The weather is currently ${description}
The temperature in London is ${temp} ° Celsius.
`);
});
});
//res.send('server is up!!!');
});
app.listen(3000, () => {
console.log("Server started!!!");
});