Configure sails.js not to cache the response

拟墨画扇 提交于 2020-01-23 07:20:30

问题


I have an application on node.js using sails.js as a framework. For displaying the informations to the user I have implemented some .ejs files.
When navigating using links within the app menu, I receive a "304 - Nod Modified" response. Also, within headers in the response I see Etag, If-None-Match or Expires.
After reading some posts, I have added the "app.disable('etag')" statement within /config/express.js file in the customMiddleware function and etag and if-none-match are not sent anymore.
Anyway, now I do not see any request made to the server (just the first one is made) when accessing different pages (not even 304 response).
How can I tell sails.js to stop caching my pages?


回答1:


You can add this to a policy instead and then just apply it to the specific pages/requests you want - if you do not want to set this for the entire application.

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



回答2:


It seems that adding the code below solved my problem:

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

These line should be included in the customMiddleware function from the /config/express.js file.

If there is a cleaner way to achieve the same result, please reply.



来源:https://stackoverflow.com/questions/24352742/configure-sails-js-not-to-cache-the-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!