ExpressJS session expiring despite activity

前端 未结 2 1586
闹比i
闹比i 2020-12-05 02:54

Bringing this question to SO since the express group didn\'t have an answer.

I\'m setting the session maxAge = 900000 and I see that the the expires property on the

相关标签:
2条回答
  • 2020-12-05 03:14

    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
    }));
    
    0 讨论(0)
  • 2020-12-05 03:26

    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();
    
    }
    
    0 讨论(0)
提交回复
热议问题