【问题标题】:Configure sails.js not to cache the response配置sails.js 不缓存响应
【发布时间】:2014-06-22 15:27:25
【问题描述】:

我有一个使用sails.js 作为框架的node.js 应用程序。为了向用户显示信息,我实现了一些 .ejs 文件。
使用应用程序菜单中的链接导航时,我收到“304 - Nod Modified”响应。此外,在响应的标题中,我看到 EtagIf-None-MatchExpires
在阅读了一些帖子后,我在 customMiddleware 的 /config/express.js 文件中添加了 "app.disable('etag')" 语句strong> 函数和 etag 和 if-none-match 不再发送。
无论如何,现在我在访问不同的页面(甚至没有 304 响应)时没有看到对服务器发出的任何请求(只是发出了第一个请求)。
如何告诉sails.js 停止缓存我的页面?

【问题讨论】:

    标签: node.js caching sails.js


    【解决方案1】:

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

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

    【讨论】:

    • 这看起来很不错,实际上。我的下一个目标是找到一种仅在development 环境下的config/env 下执行此操作的方法。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 如@c1freitas 之前提到的那样,可以使用更好的方法来获得相同的结果,但仅适用于特定页面。
    猜你喜欢
    • 2013-12-08
    • 2022-07-03
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多