简体   繁体   English

node.js http.get中的缓存控制

[英]Cache-Control in node.js http.get

I'm making requests to JSON objects with node using http.get(), it all works fine but, in some cases, I get an outdated version of the page (there's a date field that enables me to make sure). 我正在使用http.get()向带有节点的JSON对象发出请求,这一切都很好,但是在某些情况下,我得到了该页面的过时版本(有一个日期字段使我可以确定)。 The behaviour is really inconsistent, I can get the right thing one moment, and the wrong thing the next... Here's my request : 行为确实前后矛盾,我一会儿就能得到正确的东西,下一分钟我就可以得到错误的东西……这是我的要求:

var options = {host:'host.com',path:urlPath,headers:{'Cache-Control':'no-cache'}}
http.get(options, function(res){
    //JSON.parse result and check the date, sometimes 17/1, sometimes 10/1
});

Is there anything wrong with the request header? 请求标头有什么问题吗? I tried 'max-age=0' instead of 'no-cache', to no avail..., does anyone have an idea where this could come from? 我尝试使用“ max-age = 0”而不是“ no-cache”,但无济于事...,有人知道这可能来自哪里吗? In my browser, I get the last version all the time, a bit lost here, Help! 在我的浏览器中,我一直都在获取最新版本,在这里有些迷失了,帮助!

Solved it, thanks to users comment, what I did is : 解决了,感谢用户的评论,我所做的是:

urlPath+="&ie="+(new Date()).getTime();
var options = {host:'host.com',path:urlPath,headers:{'Cache-Control':'no-cache'}}
http.get(options, function(res){
    //JSON.parse result and check the date, sometimes 17/1, sometimes 10/1
});

Stupid and awesome at the same time... 愚蠢而又棒极了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM