Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect-mongo Alternative for Express 4

I am looking to implement cookiestore in my Express app, I followed this question

Best Session Storage Middleware for Express + MongoDB

and

https://github.com/kcbanner/connect-mongo

for my Express 3.x project, but for Express 4, connect middleware is deprecated.

Which is the suitable alternative for connect-mongo?

like image 787
Ashok Kumar Sahoo Avatar asked May 01 '14 10:05

Ashok Kumar Sahoo


1 Answers

Middleware has been extracted out of the core and moved individual modules. This changes how you set up the app but you have the option to use the same middleware as before. The overview explaining how to migrate from 3.x to 4.x lists the modules that can be used as replacements for the Connect middleware.

The syntax will be slightly different as you explicitly install the modules, such as express-session, and the app.use statements are modified to reflect this. The options you pass to these modules, however, are the same as it was with the Connect middleware...so you can pass your connect-mongo details to express-session and keep rolling along.

So you don't have to change unless there's another problem that isn't clear in your original question...and there could be other problems if you have a large, established app. But if you are following a tutorial, you should be early enough in the process that this won't be a major issue.

Edit: It looks like there's also been discussion about Express 4 on the connect-mongo github page. There are more examples there about how to use this module with the new version of Express...

Edit 2: The code, referenced a few times on the github page, looks like this:

var session    = require('express-session');
var MongoStore = require('connect-mongo')(session);

app.use(session({
    secret: 'youshouldputyourownsecrethere',
    store: new MongoStore({
        db              : mongoose.connection.db,
    })
}));
like image 120
Matthew Bakaitis Avatar answered Nov 16 '22 14:11

Matthew Bakaitis