为express.js更新Cookie会话

2024-02-18

我正在使用cookie会话 https://github.com/expressjs/cookie-sessionExpress.js 处理会话的模块。

我希望在每次页面加载(或ajax调用)时更新会话。这就是他们通常在任何地方工作的方式。

文档对此只字未提。

我尝试这样做,但没有成功:

app.use(function(req,res,next ){
     req.sessionOptions.maxAge = 20*1000;
     return next();
});

我怀疑您没有将响应 cookie 发送给客户端。我使用一个中间件解决了同样的问题(使用express和cookie会话),该中间件发送一个包含每个获取的不同值的cookie:

var cookieSession = require('cookie-session') 

app.use(cookieSession({
  key: cookieKey,
  secret: cookieSecret,
  maxAge: 1 * 60 * 60 * 1000 // 1 hour (rolling)
  })
);

app.get('*', function(req, res, next) {
  // To update the session expiration time we need to send the new
  // expiration in the response cookie.
  // To send again the response cookie to the client we need to
  // update the session object.
  req.session.fake = Date.now();
  next();
});

事实上,如果会话对象没有改变 https://github.com/expressjs/cookie-session/blob/1.2.0/index.js#L115,cookie-session v.1.2.0 没有设置 Set-Cookie 标头 https://github.com/expressjs/cookie-session/blob/1.2.0/index.js#L224将响应 cookie 发送到客户端。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为express.js更新Cookie会话 的相关文章

随机推荐