In express 3 I use connect-mongo
for session store.
var mongoStore = require('connect-mongo')(express);
But after I switched to express 4 it doesn't work. I got this error:
Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
I see connect has been removed from express 4. How can I continue use this or are there any good libs that I can use for express 4. Thanks.
Session data is stored server-side. Note Since version 1.5. 0, the cookie-parser middleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req / res .
Background: Normally express-session saves the session at the end of each http response, i.e. when the route is finished. However it's possible to save a session at any time with req. session. save() .
MongoDBStore. This module exports a single function which takes an instance of connect (or Express) and returns a MongoDBStore class that can be used to store sessions in MongoDB.
To store confidential session data, we can use the express-session package. It stores the session data on the server and gives the client a session ID to access the session data. In this article, we'll look at how to use it to store temporary user data.
You need to install the express-session package separately now. It can be found at https://github.com/expressjs/session
Use the following commands to get up and running:
npm install --save express-session cookie-parser
and then in your server.js file:
var express = require('express'),
cookieParser = require('cookie-parser'),
expressSession = require('express-session'),
MongoStore = require('connect-mongo')(expressSession),
app = express();
app.use(cookieParser());
app.use(expressSession({
secret: 'secret',
store: new MongoStore(),
resave: false,
saveUninitialized: true
}));
And enjoy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With