使用ExpressJS为每个文件设置Cache-Control

Bre*_*ett 2 cache-control node.js express

有没有办法在ExpressJS应用程序中为每个文件设置Cache-Control属性?我希望对我的应用程序中的文件缓存进行精细控制......我怎样才能实现这一目标?什么是关于Cache-Control的最佳实践?

Jon*_*Ong 5

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'))
Run Code Online (Sandbox Code Playgroud)

不需要让它变得复杂.