Session Node.js和Express会话处理-返回按钮问题

Session Node.js和Express会话处理-返回按钮问题,session,node.js,express,Session,Node.js,Express,我的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();

我的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');
});
。。。会话被终止,但当我在浏览器中单击“后退”按钮时,我从浏览器的缓存中获得了受限页面。这意味着没有进入“/dashboard”和用户登录验证。

我试着在meta(Jade模板)中使用no-cache,但仍然不起作用

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')

有人吗?

乔希的回答很遗憾对我不起作用。
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
  });
});
但经过一番搜索,我找到了这个 问题:

并采用了node.js/express问题的答案。 您只需更改以下行

res.header('Cache-Control', 'no-cache');

现在,每次我使用浏览器后退按钮时,页面都会被重新加载而不是缓存

*express 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

简单的解决方案是在清除会话之后。。再次重定向到同一路线。。例如:route/sessionedpage具有会话变量。。单击注销按钮后,通过
req.session.destroy(function(){})清除会话变量之后,您将尝试重定向主页。。。而不是重定向到主页。。重定向/sessionedpage(同一路线)。。。使用Express^4.16.3为/sessionedpage if(!res.sessions)编写if条件,然后使用res.redirect(“/home”)

Am,这对我来说很有效,正如@pkyeck所说的那样

我像这样将其添加到路线中,效果很好:

routes
.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();
          })
          .get('/login', function(req, res, next){
              .....
})

我也有类似的问题。你到底在哪里添加这行代码?试试这个:app.use(函数(req,res,next){res.header('Cache-Control','no Cache,private,no store,must revalidate,max stale=0,post-check=0,pre-check=0');next();})@nikjohn更新了答案,添加了express 4+@pkyeck的代码片段是的,我尝试了
res.set
,但不幸的是,我认为没有任何解决方案。这完全取决于浏览器的外观like@nikjohn这很好用,我希望你能让它为你工作,谢谢你的回答
routes
.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();
          })
          .get('/login', function(req, res, next){
              .....
})