Renewing Cookie-session for express.js

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:07:17

问题


I'm using the cookie-session module for Expresss.js to deal with sessions.

I would like the sessions to be renewed on every page load (or ajax call). That's how they usually work anywhere.

The documentation doesn't say a word about it.

I've tried doing this, but without success:

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

回答1:


I suspect that you are not sending the response cookie to the client. I solved the same problem (using express and cookie-session) with a middleware that sends a cookie containing a different value for each get:

app.use(session({
  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();
});

Indeed, if the session object does not change, cookie-session v. 1.2.0 does not set the Set-Cookie header to send the response cookie to the client.



来源:https://stackoverflow.com/questions/33169002/renewing-cookie-session-for-express-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!