最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Node.js和Express会话处理

IT培训 admin 2浏览 0评论

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();
  } 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(http-equiv='Cache-Control', content='no-store, no-cache, must-revalidate')
meta(http-equiv='Pragma', content='no-cache')
meta(http-equiv='Expires', content='-1')

任何人?

回答如下:[乔希的回答可惜对我没有用。但是经过一番搜索我发现了这个问题:What's the best way to deal with cache and the browser back button?

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

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

to

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

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();
  } 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(http-equiv='Cache-Control', content='no-store, no-cache, must-revalidate')
meta(http-equiv='Pragma', content='no-cache')
meta(http-equiv='Expires', content='-1')

任何人?

回答如下:[乔希的回答可惜对我没有用。但是经过一番搜索我发现了这个问题:What's the best way to deal with cache and the browser back button?

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

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

to

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论