Connect-mongo sessions not being deleted automatically

前端 未结 2 1250
一个人的身影
一个人的身影 2021-01-31 21:29

I have an application using NodeJS, Express, MongoDB and connect-mongo.

My issue is that sessions don’t seem to be automatically deleted from MongoDB when they expire, s

2条回答
  •  暖寄归人
    2021-01-31 22:23

    You haven't set a clear_interval for your sessions .. the connect-mongo default is -1 (or "never"):

    clear_interval Interval in seconds to clear expired sessions (optional, default: -1). Values <= 0 disable expired session clearing.

    Example of removing expired sessions every hour (3600s):

    var sessionStore = new MongoStore({
         db: 'myappsession',
         clear_interval: 3600
    });
    

    You should also make sure you have set a maxAge on your sessions to they actually expire (eg using 1 day):

    app.use(express.session({
        secret: "myappsecret",
        cookie: { maxAge: 24 * 60 * 60 * 1000 },
        store:sessionStore
    }));
    

提交回复
热议问题