尽管有活动,ExpressJS 会话仍将过期 [英] ExpressJS session expiring despite activity

查看:40
本文介绍了尽管有活动,ExpressJS 会话仍将过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提出这个问题,因为 express 小组没有答案.

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

我正在设置会话 maxAge = 900000,我看到会话 cookie 上的 expires 属性设置正确.但是,在后续请求中,不会延长超时时间.它永远不会被扩展,cookie 最终会过期.

I'm setting the session maxAge = 900000 and I see that the the expires property on the session cookie is set correctly. However, on subsequent requests the timeout is not being extended. It is never extended and the cookie eventually expires.

会话中间件文档说 Session#touch()没有必要,因为会话中间件会为我做这件事.我实际上尝试手动调用 req.session.touch() 并且什么也没做,我也尝试在 req.session.cookie 上设置 maxAge 并且什么也没做:-(

The session middleware docs say that Session#touch() isn't necessary because the session middleware will do it for me. I actually tried calling req.session.touch() manually and that did nothing, I also tried setting the maxAge on the req.session.cookie as well and that did nothing :-(

我是否在某处缺少自动扩展活动会话的设置?除了在每个请求上手动重新创建 cookie 之外,还有没有其他方法可以在最终用户活动后延长会话超时时间?

Am I missing a setting somewhere to automatically extend active sessions? Short of recreating the cookie manually on each request is there any other way to extend a session timeout after end-user activity?

我在 express v3 中遇到了这个问题.我不是 100% 确定,但我认为来自 快速变更日志的这个注释可能是罪魁祸首:

I experienced this problem in express v3. I'm not 100% sure but I think this note from the express changelog may have been the culprit:

推荐答案

这里是解决方案,以防其他人遇到同样的问题:

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();

}

这篇关于尽管有活动,ExpressJS 会话仍将过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆