繁体   English   中英

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

[英]app with sails.js no cache browser

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

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();

config/http.coffee ,添加一个“ nocache”中间件。 在咖啡脚本中:

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并禁用所有内容的缓存。

可以在config / policies.js中添加策略

'*': 'noCachePolicy'

http://sailsjs.org/#/documentation/concepts/Policies但是,以上方法要求您的所有视图都必须在控制器中定义一个操作。 否则,策略将无法以这种方式工作。

  • 要么 -

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

  • 要么 -

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM