ExpressJS session expiring despite activity ExpressJS session expiring despite activity express express

ExpressJS session expiring despite activity


Rolling sessions now exist in express sessions. Setting the rolling attribute to true in the options, it will recalculate the expiry value by setting the maxAge offset, applied to the current time.

https://github.com/expressjs/session/issues/3

https://github.com/expressjs/session/issues/33

https://github.com/expressjs/session (search for rolling)

For example, note the rolling:

app.use(session({  secret: 'a secret',  cookie: {    path: '/',    httpOnly: true,    secure: false,    maxAge: 10 * 60 * 1000  },  rolling: true}));


Here is the solution in case anyone else has the same issue:

function (req, res, next) {    if ('HEAD' == req.method || 'OPTIONS' == req.method) return next();    // break session hash / force express to spit out a new cookie once per second at most    req.session._garbage = Date();    req.session.touch();    next();}