node.js - 使用 ExpressJS 为每个文件设置缓存控制

标签 node.js express cache-control

有没有办法在您的 ExpressJS 应用程序中为每个文件设置 Cache-Control 属性?我想对我的应用程序中的文件缓存进行精细控制...我该如何实现? 关于缓存控制的最佳实践是什么?

最佳答案

app.use(function (req, res, next) {
  if (req.url.match(/* some filter */)) {
    res.setHeader('Cache-Control', ...)
  } else if (req.url.match(/* some filter */)) {
    res.setHeader('Cache-Control', ...)
  }

  next()
})

app.use(express.static(__dirname + '/public'))

没必要把它弄复杂。

关于node.js - 使用 ExpressJS 为每个文件设置缓存控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493248/

相关文章:

javascript - Node.js Socket.io 错误 : disconnect event is triggered after connect

node.js - 如何断言函数被调用?

node.js - 重试连接 postgres Nodejs

node.js - 尝试获取 openshift/nodejs 上已安装模块的列表时出错

javascript - 在执行 MEAN 教程时,当 'resolve' 被引入 $stateProvider 状态时,内联 HTML 停止工作

javascript - Typescript & Express : Export several routes from single index. ts 文件以 ''/api/v1 为前缀”

php - 仅限浏览器的缓存控制 - 不是 CDN

java - 如何在 Java 服务器页面中使用 Cache-Control 和 Expires header

http - http请求中的`cache-control: max-age=0`

node.js - sudo npm install -g Node 检查器错误