配置sails.js不缓存响应

时间:2014-06-22 15:27:26

标签: node.js caching sails.js

我在node.js上有一个使用sails.js作为框架的应用程序。为了向用户显示信息,我实现了一些.ejs文件 当使用应用程序菜单中的链接导航时,我收到“304 - Nod Modified”响应。此外,在回复标题中,我看到 Etag 如果 - 无匹配过期
阅读了一些帖子后,我在 customMiddleware js 文件中添加了“app.disable('etag')”语句功能和etag和if-none-match不再发送。
无论如何,现在我在访问不同的页面时看不到对服务器的任何请求(只是第一个请求)(即使是304响应)。
我如何告诉sails.js停止缓存我的页面?

2 个答案:

答案 0 :(得分:11)

您可以将其添加到策略中,然后将其应用于您想要的特定页面/请求 - 如果您不想为整个应用程序设置此项。

/api/policies/nocache.js:

/**
 * Sets no-cache header in response.
 */
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();
};

答案 1 :(得分:1)

似乎添加下面的代码解决了我的问题:

res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');  

这些行应包含在/ config / express.js 文件的 customMiddleware 函数中。

如果有更简洁的方法可以达到相同的效果,请回复。