我正在使用 Node js、express、request 和 ejs。以下代码有效,但 JSON 数据未更新。当我查看原始 JSON URL 时,我得到的数据与我的应用程序显示的数据不同。
app.js 文件:
var express = require("express");
var app = express();
var request = require("request");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.get("/", function(req, res){
const weatherURL = "https://api.aerisapi.com/forecasts/:auto?&format=json&limit=1&client_id=" + apiId + "&client_secret=" + apiSecret;
request(weatherURL, function(error, response, body){
let weather_json = JSON.parse(body);
const weather = {
forecast : weather_json.response[0].periods[0].weather,
temp: weather_json.response[0].periods[0].feelslikeF,
icon : weather_json.response[0].periods[0].icon
};
res.render("index", {weather: weather});
});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("App has Started!");
});
当数据本应为 62° 多云时,我的应用程序却出现了 65° 多云和孤立 Storm 。
也许 JSON 被缓存了?
来自网址的 JSON:
{"success":true,"error":null,"response":[{"loc":{"long":-83.814,"lat":35.341},"interval":"daynight","periods":[{"timestamp":1538650800,"validTime":"2018-10-04T07:00:00-04:00","dateTimeISO":"2018-10-04T07:00:00-04:00","maxTempC":29,"maxTempF":85,"minTempC":null,"minTempF":null,"avgTempC":25,"avgTempF":77,"tempC":null,"tempF":null,"pop":7,"precipMM":0,"precipIN":0,"iceaccum":0,"iceaccumMM":0,"iceaccumIN":0,"maxHumidity":92,"minHumidity":60,"humidity":70,"uvi":7,"pressureMB":1019,"pressureIN":30.09,"sky":43,"snowCM":0,"snowIN":0,"feelslikeC":17,"feelslikeF":62,"minFeelslikeC":17,"minFeelslikeF":62,"maxFeelslikeC":31,"maxFeelslikeF":88,"avgFeelslikeC":26,"avgFeelslikeF":79,"dewpointC":17,"dewpointF":62,"maxDewpointC":20,"maxDewpointF":68,"minDewpointC":17,"minDewpointF":62,"avgDewpointC":19,"avgDewpointF":66,"windDirDEG":150,"windDir":"SSE","windDirMaxDEG":290,"windDirMax":"WNW","windDirMinDEG":280,"windDirMin":"W","windGustKTS":6,"windGustKPH":11,"windGustMPH":7,"windSpeedKTS":2,"windSpeedKPH":4,"windSpeedMPH":3,"windSpeedMaxKTS":4,"windSpeedMaxKPH":8,"windSpeedMaxMPH":5,"windSpeedMinKTS":1,"windSpeedMinKPH":2,"windSpeedMinMPH":1,"windDir80mDEG":224,"windDir80m":"SW","windDirMax80mDEG":290,"windDirMax80m":"WNW","windDirMin80mDEG":280,"windDirMin80m":"W","windGust80mKTS":5,"windGust80mKPH":8,"windGust80mMPH":5,"windSpeed80mKTS":4,"windSpeed80mKPH":7,"windSpeed80mMPH":4,"windSpeedMax80mKTS":5,"windSpeedMax80mKPH":8,"windSpeedMax80mMPH":5,"windSpeedMin80mKTS":2,"windSpeedMin80mKPH":3,"windSpeedMin80mMPH":2,"weather":"Partly Cloudy","weatherCoded":[{"timestamp":1538650800,"wx":"PA::F","dateTimeISO":"2018-10-04T07:00:00-04:00"}],"weatherPrimary":"Partly Cloudy","weatherPrimaryCoded":"::SC","cloudsCoded":"SC","icon":"pcloudy.png","isDay":true}],"profile":{"tz":"America\/New_York"}}]}
最佳答案
如果您认为数据正在被缓存,您可以尝试在查询参数中添加时间戳。像这样:
const weatherURL = "https://api.aerisapi.com/forecasts/:auto?&format=json&limit=1&client_id=" + apiId + "&client_secret=" + apiSecret + "&t=" + Date.now();
关于node.js - Node js express请求天气API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640723/