使用sails.js的应用程序没有缓存浏览器

时间:2015-01-28 15:49:26

标签: javascript node.js sails.js

我希望我的应用程序不会保存在浏览器缓存中。我有这个政策,但如何应用我的所有观点?

module.exports = function (req, res, next) {
sails.log.info("Applying disable cache policy");
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');  
next();

2 个答案:

答案 0 :(得分:3)

config/http.coffee中,添加" nocache"中间件。在coffeescript中:

module.exports.http = middleware:
  order: [
    ...
    "nocache"
    "router"
    "www"
    "favicon"
    "404"
    "500"
  ]

  nocache: (req, res, next) ->
    if req.path.indexOf("/api") is 0
      res.header "Cache-Control", "private, no-cache, no-store, must-revalidate"
      res.header "Expires", "-1"
      res.header "Pragma", "no-cache"
    next()

在这种情况下,它会缓存除/api路径下的调用之外的所有内容。您可以简单地删除所有内容的if和disable缓存。

答案 1 :(得分:1)

可以在config / policies.js

中添加策略
'*': 'noCachePolicy'

http://sailsjs.org/#/documentation/concepts/Policies 但是,上述方法要求您的所有视图都在控制器中定义了一个操作。否则政策不会以这种方式运作。

  • 或 -

它们可以直接附在路线上 http://sailsjs.org/#/documentation/concepts/Routes/RouteTargetSyntax.html 但是,此方法要求您写出所有路线。

  • 或 -

如果您希望将此附加到所有路由,我认为您最好的方法,查看它以创建您自己的中间件并将其放在那里。这样你就不必处理上述两个问题。 http://sailsjs.org/#/documentation/concepts/Middleware