使用 SO 已经有一段时间了,但这是我在这里的第一个问题,因为我找不到这个特定问题的解决方案。我会尽量说得清楚一些,有什么不清楚的就告诉我,所以我会相应地更新问题。
我正在使用路由根据路由中作为参数()传递的 userId 来提供用户的照片。它可以工作,但是每次遇到标签时,它都会从服务器请求图像并且从不使用缓存版本。
我玩了一点 Cache-Control,但要么全有,要么全无:如果启用了缓存并且用户更改了他的照片,它仍将使用旧照片。
我的问题是:如果照片没有更改,是否可以使用缓存版本,但如果它确实使用了服务器中的那个?我尝试使用“must-revalidate”以及“max-age”或“no-cache”无济于事。
如果有帮助,这是路线代码:
Module.exports.getPhoto = function(req, res) {
var mime = require('mime-magic'),
memberId = req.params.memberId,
imgUrl;
path.exists('public/images/memberPhotos/' + memberId, function(exists) {
if(exists) {
imgUrl = 'public/images/memberPhotos/' + memberId;
}else{
imgUrl = 'public/images/memberPhotos/noPhoto.jpg';
}
fs.readFile(imgUrl, function(err, img) {
mime.fileWrapper(imgUrl, function(err, mimeType) {
if(!err) {
console.log(mimeType);
res.writeHead(200, {
'Content-Type': mimeType,
'Cache-Control': "max-age=" + 43800*60 + ", must-revalidate"
});
res.end(img, 'binary');
}
});
});
});
};
谢谢