Node.js和Express会话处理 – 后退button问题
我的Express应用程序中有一个限制区域“/ dashboard”。 我使用一个非常小的函数来限制访问:
app.get('/dashboard', loadUser, function(req, res){ res.render('dashboard', { username: req.session.username }); }); function loadUser(req, res, next){ if (req.session.auth) { next(); } else { res.redirect('/login'); } };
问题是,当我通过调用注销用户…
app.get('/logout', function(req, res){ if (req.session) { req.session.auth = null; res.clearCookie('auth'); req.session.destroy(function() {}); } res.redirect('/login'); });
…会话被杀害, 但当我在浏览器中点击“后退”button时,浏览器的caching中就有了受限制的页面。 这意味着“/仪表板”上没有GET,也没有用户loginvalidation。
我尝试在元(Jade模板)中使用nocaching但它仍然无法正常工作。
meta(http-equiv='Cache-Control', content='no-store, no-cache, must-revalidate') meta(http-equiv='Pragma', content='no-cache') meta(http-equiv='Expires', content='-1')
任何人?
乔希的回答可悲的是没有为我工作。 但经过一番search,我发现这个问题: 什么是处理caching和浏览器后退button的最佳方式?
并通过了这个node.js / express问题的答案。 您只需更改以下行
res.header('Cache-Control', 'no-cache');
至
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
现在,每次我使用浏览器后退button,页面被重新加载,而不是caching。
*更新为快速v4.x *
// caching disabled for every route server.use(function(req, res, next) { res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'); next(); }); // otherwise put the res.set() call into the route-handler you want
app.get('/dashboard', loadUser, function(req, res){ res.header('Cache-Control', 'no-cache'); res.header('Expires', 'Fri, 31 Dec 1998 12:00:00 GMT'); res.render('dashboard', { username: req.session.username }); });
不要介意我以前的文章,看到这个问题: 最简单的方法来防止用户注销后,浏览器的后退button显示安全的数据?