简体   繁体   English

Express服务器在Express会话中不活动后的会话超时

[英]Session timeout after inactivity in express-session in Express server

I want to expire the session after 1 min of inactivity. 我要在闲置1分钟后使会话过期。 I tried the following 我尝试了以下

 app.use(session({
     secret: config.sessionSecret,
     cookie: { maxAge: 60000 },
     store: new mongoStore({
        db: db.connection.db,
        collection: config.sessionCollection
     })
 }));

The session is expiring exactly after 1 min the user logs in even if the user continues to use the application. 即使用户继续使用该应用程序,会话也会在用户登录1分钟后完全到期。 I have been unable to set it to expire after 1 min of inactivity . 闲置1分钟后,我无法将其设置为过期。 Any ideas? 有任何想法吗? Thanks in advance again. 再次感谢。

I'm not sure if you've resolved your issue but I had the same issue and I'd like to post what fixed it here in case it can benefit anyone. 我不确定您是否已解决问题,但我也遇到了同样的问题,我想在此发布修正的内容,以防它对任何人都有利。

It seems that the default expires will end the session after the specified length of time regardless of what the user is doing but if you include resave and rolling variables it will only expire after it has been idle for the specified length of time. 似乎默认过期时间将在指定的时间长度后结束会话,而不管用户在做什么,但是如果您包含resave和rolling变量,则仅在它闲置了指定的时间长度后,该过期时间才会终止。

var express = require("express");
var session = require("express-session");

var app = express();

app.use (
    session ({
        secret: "53cr3t50m3th1ng",
        resave: true,
        rolling: true,
        saveUninitialized: false,
        cookie: {
            expires: 30 * 1000
        }
    })
);

This is a bug in connect. 这是连接中的错误。 See this issue: 看到这个问题:

https://github.com/senchalabs/connect/issues/670 https://github.com/senchalabs/connect/issues/670

I know it too late to answer on this query but may it will help someone else. 我知道现在回答这个查询为时已晚,但可能会对其他人有所帮助。

const session = require('express-session');
app.use(session({
secret: 'secret',
name: 'farmerApp',
resave: true,
saveUninitialized: true,
cookie: {
    expires: 120000
}
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM