为注销的用户有条件地设置Cache-control
标题no-cache
app.use(function(req, res, next) {
if (!req.user)
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
next();
});
这将强制浏览器获取页面的新副本,即使他们点击“返回”。
注意:这是以禁用所有未登录用户的缓存为代价的,为了回答这个问题,包括刚刚注销的用户。如果您不想为所有已注销的用户完全禁用缓存,您可能应该找到一种区分两者的方法。有会话的东西..
如果您确定当用户回击时,'/login'
是他们将降落的路线,那么您可以仅在此处定义它,从而避免执行上述操作的麻烦。
这段代码到底去哪儿了?
app.get('/logout', isLoggedIn, function(req, res) {
req.logOut();
if (!req.user)
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.redirect('/login');
});
可以这样使用吗?
不。
app.get
(或app.use
)定义您的路线。文档: http ://expressjs.com/api.html#request
app.get('/logout'...
只会_'/logout'
当客户端请求执行。
app.use(...)
(不指定任何路由)将对所有请求执行。
这些路由“中间件”(它们被称为)也被一个接一个地执行。(您将在上述文档中了解更多信息)
您想在任何其他路由之前设置标头,以便无论其他路由呈现什么,都会使用强制使用户缓存无效的标头呈现。
// > HERE <
// before all the other routes
app.get('/logout'...
app.get('/login'...
app.get('/'...
app.get('/stuff'...