Session lifetime in node.js with expression and MongoDB

I am using node.js with a pronounced structure. As a session repository, I use MongoDB. How to set the lifetime after which session objects are deleted from MongoDB. Here's how I make an announcement:

app.use(express.cookieParser()); app.use(express.session({ secret: "Stays my secret", store: new MongoStore({ db: 'myDB' }) })); 
+7
source share
2 answers

Your question is a bit vague, but from what I can collect, you do not want to set a session end period:

you can use maxAge like this:

 app.use(express.cookieParser()); app.use(express.session({ secret : "Stays my secret", maxAge : new Date(Date.now() + 3600000), //1 Hour expires : new Date(Date.now() + 3600000), //1 Hour store : new MongoStore({ db: 'myDB' }) })); 

expires value is required for new versions of express, where maxAge for older versions, you only need expires .

+7
source

@RobertPitt edit your answer. The cookie (session) looks like this:

 { "cookie":"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}, "your_var_1":"value 1", "your_var_2":"value 2", ... } 

The argument for express.session should look like this (this is in the documentation):

 app.use(express.session({ secret : "Stays my secret", cookie: { maxAge : new Date(Date.now() + 3600000), //1 Hour expires : new Date(Date.now() + 3600000), //1 Hour }, store : new MongoStore({ db: 'myDB' }) })); 

Besides:

 maxAge : new Date(Date.now() + 3600000), //1 Hour 

will lead to the fact that each cookie (here and the session) will expire automatically an hour after the server starts

0
source

All Articles