【发布时间】:2021-02-19 07:37:12
【问题描述】:
我需要从中提取天气 API 数据
https://api.weatherapi.com/v1/current.json?key=f5f45b956fc64a2482370828211902&q=London
当粘贴在网络浏览器和邮递员中时,它会给出响应。但是,一旦合并到 javascript https.get 中,它会因不断加载浏览器并用不需要的信息挂起终端而失败。
app.js:
//jshint esversion:6
const express = require("express");
const https = require("https");
const app = express();
app.get("/",function(req, res){
const url = "https://api.weatherapi.com/v1/current.json?key=f5f45b956fc64a2482370828211902&q=London";
https.get(url,function(response){
//console.log(response);
response.on("data",function(data){
const weatherData = JSON.parse(data);
const temp = weatherData.current.temp_c;
const weatherDescription = weatherData.current.condition.text;
});
});
res.send("Server is up and running");
});
app.listen(3000, function(){
console.log("Started on port 3000");
});
【问题讨论】:
标签: javascript express https