【问题标题】:Node.js and Express session handling - Back button problemNode.js 和 Express 会话处理 - 后退按钮问题
【发布时间】:2011-08-31 02:08:37
【问题描述】:

我的 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”上没有 GET,也没有用户登录验证。

我尝试在 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')

有人吗?

【问题讨论】:

    标签: session node.js express


    【解决方案1】:

    遗憾的是,乔希的回答对我不起作用。 但经过一番搜索,我发现了这个 问题:What's the best way to deal with cache and the browser back 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');
    

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

    * 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
    

    【讨论】:

    • 我遇到了类似的问题。你到底在哪里添加那行代码?
    • 试试这个:app.use(function(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+ 的 sn-p
    • @pkyeck 是的,我尝试了res.set,但不幸的是,我认为没有针对此问题的 any 解决方案。这完全取决于它看起来像的浏览器
    • @nikjohn 这很好用,我希望你能用它,谢谢菲尔的回答
    【解决方案2】:
    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
      });
    });
    

    【讨论】:

      【解决方案3】:

      我正在使用 Express ^4.16.3,正如@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){
                    .....
      })
      

      【讨论】:

        【解决方案4】:

        简单的解决方案是在清除会话之后..再次重定向到相同的路由..例如:路由 /sessionedpage 有会话变量..单击注销按钮后通过req.session.destroy(function() {}); 清除会话变量之后,您将尝试重定向主页页面...而不是重定向到主页..重定向/sessionedpage(相同的路线)...为/sessionedpage编写if条件 if(!res.sessions) then res.redirect('/home')

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-20
          • 2012-12-09
          • 1970-01-01
          • 1970-01-01
          • 2011-07-02
          • 2013-04-22
          • 1970-01-01
          • 2012-05-07
          相关资源
          最近更新 更多