Node.js服务器中没有缓存 [英] No cache in Node.js server
本文介绍了Node.js服务器中没有缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经读到要避免在Node.js中进行缓存,必须使用:
res。 header('Cache-Control','no-cache,private,no-store,must-revalidate,max-stale = 0,post-check = 0,pre-check = 0');
但是我不知道如何使用它,因为当我在代码中放入该行时会出错。 p>
我的功能(我认为我必须对Cache-Control标头进行编程)是:
function getFile(localPath,mimeType,res){
fs.readFile(localPath,function(err,contents){
if(!err){
res.writeHead(200 ,{
Content-Type:mimeType,
Content-Length:contents.length,
Accept-Ranges: bytes,
});
// res.header('Cache-Control','no-cache');
res.end(contents);
} else {
res。 writeHead(500);
res.end();
}
});
}
有人知道如何在我的代码中不放置缓存吗?
解决方案
您已经编写了标头。我认为您完成此操作后无法添加更多内容,因此只需将标题放在第一个对象中即可。
res.writeHead(200,{
'Content-Type':mimeType,
'Content-Length':contents.length,
'Accept-Ranges':'bytes',
'Cache-Control':'no-cache'
});
I have read that to avoid caching in Node.js, it is necessary to use:
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
But I don't know how to use it because I get errors when I put that line in my code.
My function (where I think I have to program the Cache-Control header) is:
function getFile(localPath, mimeType, res) {
fs.readFile(localPath, function(err, contents) {
if (!err) {
res.writeHead(200, {
"Content-Type": mimeType,
"Content-Length": contents.length,
"Accept-Ranges": "bytes",
});
// res.header('Cache-Control', 'no-cache');
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}
Does anyone know how to put no cache in my code?
解决方案
You've already written your headers. I don't think you can add more after you've done that, so just put your headers in your first object.
res.writeHead(200, {
'Content-Type': mimeType,
'Content-Length': contents.length,
'Accept-Ranges': 'bytes',
'Cache-Control': 'no-cache'
});
这篇关于Node.js服务器中没有缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文