Node js express request weather API

时间:2018-10-04 06:53:42

标签: node.js api express ejs

I am working with node js, express, request, and ejs. The following code works but the JSON data isn't updating. When I view the raw JSON URL I'm not getting the same data as my application is showing.

app.js file:

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!");
});

I am getting Partly Cloudy with Isolated Storms 65° in my application when the data should be Partly Cloudy 62°.

Maybe the JSON is getting cached?

JSON from the url:

{"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"}}]}

2 个答案:

答案 0 :(得分:0)

如果您认为要缓存数据,则可以尝试在查询参数中添加时间戳。像这样:

const weatherURL = "https://api.aerisapi.com/forecasts/:auto?&format=json&limit=1&client_id=" + apiId + "&client_secret=" + apiSecret + "&t=" + Date.now();

答案 1 :(得分:0)

我正在尝试重现您的问题,但始终无法在原始json与视图中显示的数据之间进行匹配

enter image description here

也许这与ejs缓存有关,您是否尝试添加此行?

app.disable('view cache');