Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session lifetime in node.js with express and MongoDB

I am using node.js with the express framework. As a session store I am using MongoDB. How can I set the lifetime after which the session objects are removed from MongoDB. This is how I am doing the declaration:

app.use(express.cookieParser());
    app.use(express.session({
                secret: "Stays my secret",
                store: new MongoStore({ db: 'myDB' })
                    }));
like image 924
Thomas Avatar asked Apr 17 '11 22:04

Thomas


1 Answers

Your question is a little vague but from what i can gather you wan't to set the expiration period for the session:

you can use maxAge like so:

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 as maxAge is for older versions, you should only need expires though.

like image 107
RobertPitt Avatar answered Sep 28 '22 13:09

RobertPitt