【问题标题】:I have updated the data on github but node module request still get old data我已经更新了 github 上的数据,但是节点模块请求仍然得到旧数据
【发布时间】:2016-03-24 00:04:33
【问题描述】:

我在github上放了一些数据。
我使用节点模块“请求”从中获取数据。
在我在 github 上更新我的数据之后。
nodejs 仍然可以获取大约五分钟的旧数据。
这是我的代码的一部分。

var url = "https://raw.githubusercontent.com/Larry850806/facebook-chat-bot/master/db.json";
request({ url: url, json: true }, function(error, response, body){
    if (!error && response.statusCode === 200) {
        console.log(body); // Print the json response
        // after I update data, body still get old data
    }
});

我认为这是因为有缓存。
所以我不能得到“真实”的数据,而是旧的。
有没有办法获取最新的数据?

【问题讨论】:

    标签: node.js caching request


    【解决方案1】:

    确实有一个 Github 缓存。您可能想尝试的一件事是将随机查询字符串附加到您请求的文件的末尾。

    例如:

    var url = "https://raw.githubusercontent.com/Larry850806/facebook-chat-bot/master/db.json?random=<randomnumberhere>";
    request({ url: url, json: true }, function(error, response, body){
        if (!error && response.statusCode === 200) {
            console.log(body); // Print the json response
            // after I update data, body still get old data
        }
    });
    

    这有时会“强制”后端服务器中断缓存(如果他们正在寻找查询字符串)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2017-12-29
      • 2013-12-28
      • 2019-01-26
      • 2020-10-09
      相关资源
      最近更新 更多