node.js - 浏览器缓存 Express Nodejs

标签 node.js express caching browser-cache

我正在尝试在 my website 上设置浏览器缓存没有任何成功 我的 server.js 文件中有我的代码

app.use(function(req, res, next){
res.set({
    Expires: new Date(Date.now() + 2592000000).toUTCString()
});
next();
})

app.use(express.static(path.join(__dirname, '../build/public'),{maxAge:2592000000}));

怎么了?

最佳答案

问题在于 express.staticmaxAge 选项使用Cache-Control header 。

maxAge: Set the max-age property of the Cache-Control header in milliseconds or a string in ms format.

Expires header 文档您可以找到以下内容:

If there is a Cache-Control header with the "max-age" or "s-maxage" directive in the response, the Expires header is ignored

因此,一个可能的解决方案是仅使用 Cache-Control header 。接下来的片段在快速中间件中设置此 header :

app.use((req, res, next) => {
    res.header('Cache-Control', 'max-age=2592000000');
    next();
});

关于node.js - 浏览器缓存 Express Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44437977/

相关文章:

javascript - JS/Jquery : string to words text-segmentation script using dictionary and longest match?

javascript - 所有的 JavaScript 回调都是异步的吗?如果不是,我怎么知道哪些是?

javascript - 主体解析器未定义,req.body

ruby-on-rails-3 - 使Rails缓存中的特定模型无效

caching - 利用浏览器缓存 使用 S3 Amazon

node.js - 将原始数据上传到 Google Cloud 函数中的 Firebase 存储桶?

node.js - 错误 : sorry, 已经有太多客户端

node.js - expressjs路由匹配特定的url

javascript - EXPRESS:Router.use() 需要一个中间件函数,但得到一个对象

java - 如何检查 Ehcache 2.10.x 中的过期条目