【问题标题】:Node js express request weather APINode js快递请求天气API
【发布时间】:2019-03-09 11:48:15
【问题描述】:

我正在使用 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° 孤立风暴。

也许 JSON 被缓存了?

来自 url 的 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"}}]}

【问题讨论】:

  • 你能在 res.render 之前的 console.log(weather) 并包含控制台打印输出吗?
  • consolelog: { 预报:'局部多云,有零星阵雨',温度:65,图标:'pcloudyr.png' } { 预报:'局部多云,有阵雨',温度:65,图标: 'pcloudyr.png' } 我不知道为什么它的控制台记录了两次,但它确实
  • JSON url: "feelslikeF":61, "weather":"Partly Cloudy", "icon":"pcloudy.png"
  • 当您说“来自 url 的 JSON”时,您的意思是您通过 curlpostman 或类似方式运行请求 url?
  • 嗯,我想知道由于用户代理或它可以从浏览器中提取的某些元数据,aerisapi 是否以不同的响应响应 GET 请求。你能做到curl URL 并通过命令行的输出吗?如果您的机器上没有 curl,您可以将 console.log 修改为打印输出 weather_json.response[0].loc 并将其与您从浏览器中获得的结果进行比较吗?

标签: node.js api express ejs


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但这并没有成功。我在另一台计算机上运行该应用程序,所以它一定不是浏览器缓存。
【解决方案2】:

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

可能这和ejs缓存有关,你有没有试过加这行?

app.disable('view cache');

【讨论】:

  • 感谢您的帮助!我想到了。当我在云 9 上时,地理位置一定不能正常工作。我得到的位置是 { long: -77.25, lat: 38.658 }
猜你喜欢
  • 2017-05-04
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多