39

Right now i'm storing my session data in the "memory store" which comes bundled with connect(express). But I want/have to change this for production.

The application is using mongodb and I installed mongoose to handle all db-communications.

e.g. Connect to the DB after initializing my app:

var mongo = require('mongoose');
mongo.connect('mongodb://localhost/myDb');
mongo.connection.on('open', function () {
  app.listen(3000);
}

I found the connect-mongodb module, but I don't know how to implement it using mongoose, or if it's actually possible? I need to add something like this:

var mongoStore = require('connect-mongodb');
// ...
app.use(express.session({
  secret: 'topsecret',
  maxAge: new Date(Date.now() + 3600000),
  store: new mongoStore({ db: 'myDb' })
}));

or do I have to define my db connection a second time using the mongodb-module directly?

7 Answers 7

43

in the end i'm using a bit of every answer that was given before:

  • i switched from connect-mongodb to connect-mongo module
  • i'm using a general conf object to store my configuration data
  • there are two db connections because it's easier to handle for me (maybe changed later on, if/when a new version of mongoose/express comes out)

requirements:

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

conf object:

var conf = {
  db: {
    db: 'myDb',
    host: '192.168.1.111',
    port: 6646,  // optional, default: 27017
    username: 'admin', // optional
    password: 'secret', // optional
    collection: 'mySessions' // optional, default: sessions
  },
  secret: '076ee61d63aa10a125ea872411e433b9'
};

then i can configure it like this:

app.configure(function(){
  // ...
  app.use(express.cookieParser());
  app.use(express.session({
    secret: conf.secret,
    maxAge: new Date(Date.now() + 3600000),
    store: new MongoStore(conf.db)
  }));
  // important that this comes after session management
  app.use(app.router);
  // ...
});

and finally connect to mongodb a second time using mongoose:

var dbUrl = 'mongodb://';
dbUrl += conf.db.username + ':' + conf.db.password + '@';
dbUrl += conf.db.host + ':' + conf.db.port;
dbUrl += '/' + conf.db.db;
mongo.connect(dbUrl);
mongo.connection.on('open', function () {
  app.listen(3000);
});
6
  • Good work! Only addition I have that was compounding my troubles is to make sure that your "app.use(app.router);" is placed after the "app.use(express.session...". Aug 30, 2011 at 22:53
  • 9
    For anyone trying to get this working recently: it seems that as of v0.1.9, you'll need to set express as a varilable (var express = require('express')) and then pass that to connect-mongo (var MongoStore = require('connect-mongo')(express)). This works because express exposes all of the underlying parts of connect, and connect-mongo is actually a function that takes the connect object as an argument. May 29, 2012 at 1:40
  • In order to avoid EADDRINUSE errors I had to remove the "app.listen(3000)" line from the bottom section of code that you had, because I was already doing my .listen somewhere else. That part only needs to be done once and is for starting the actual express webserver itself -- not relevant per se to the mongodb connection. Just a note!
    – CommaToast
    Jan 10, 2013 at 8:19
  • Okay, I reached till here, but now how do I interact with the session? set and get values..
    – Hadesara
    Mar 22, 2013 at 21:24
  • @Hadesara There is excellent documentation on expressjs and sessions here: expressjs-book.com/forums/topic/…
    – ngChris
    Jun 22, 2014 at 2:47
27

Please include

app.use(express.cookieParser());

directly before

app.use(express.session({

Otherwise throws error as below,

TypeError: Cannot read property 'connect.sid' of undefined

2
  • 3
    That saves lives. The internet is wrong on this - there wasn't a single example from the top of the results even suggesting about this.
    – antitoxic
    Oct 4, 2012 at 20:51
  • 3
    thanks for the hint but next time, just edit my answer and add the missing line so that others won't have to read another answer ... Jan 10, 2013 at 11:06
7

It looks like you could do this to setup connect-mongodb assuming your mongoose connection code above is run earlier:

app.use(express.session({
  secret: 'topsecret',
  maxAge: new Date(Date.now() + 3600000),
  store: new mongoStore({ db: mongoose.connections[0].db })
}));
2
  • i'm using the express.session() in app.configure() before i connect to the DB. i think, i'll define a config object which contains all DB info and try to use connect-mongo (instead of connect-mongodb) - implementation looks easier than connect-mongodb Jul 25, 2011 at 18:50
  • the example in the github page doesn't tell how to use with mongoose. this example helped a lot.
    – mauris
    Dec 11, 2014 at 14:27
3

So connect-mongodb does not use Mongoose, it uses the node-mongodb-native driver (i.e.: npm install mongodb). Mongoose also depends on this driver, so it should be present.

Looking at the code directly, you have to provide your DB connection information as a MongoStore object:

store: new mongoStore({ host: 'session_server', port: 27017, db: 'seesion', collection: 'sessions' })

Typically for this, you'll want to have some "config" object or variable that can be dynamically loaded (dev vs test vs prod). Then you pull the host/port/db/auth off of that config object.

1
  • you link to connect-mongo, i used connect-mongodb in the example. but they're almost identical, so i will check this out. Jul 25, 2011 at 18:43
3

For express 4x:

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

var conf = {
  db: {
    db: 'myDb',
    host: '192.168.1.111',
    port: 6646,  // optional, default: 27017
    username: 'admin', // optional
    password: 'secret', // optional
    collection: 'mySessions' // optional, default: sessions
  },
  secret: '076ee61d63aa10a125ea872411e433b9'
};

app.configure(function(){
  app.use(express.cookieParser());
  app.use(session({
    secret: conf.secret,
    maxAge: new Date(Date.now() + 3600000),
    store: new MongoStore(conf.db)
  }));
});

var dbUrl = 'mongodb://';
dbUrl += conf.db.username + ':' + conf.db.password + '@';
dbUrl += conf.db.host + ':' + conf.db.port;
dbUrl += '/' + conf.db.db;
mongo.connect(dbUrl);
mongo.connection.on('open', function () {
  app.listen(3000);
});

session has been moved to it's own module, so you need to require it and use session when configuring the MongoStore.

1
  • In line 3, what exactly it means by (session) after require statement. Is it like (session) is passed to mongoStore
    – ajay_t
    Oct 24, 2016 at 3:31
1

You can pass in an object of connection details (host, username, password, etc.).

You can also pass in a connection url like mongodb://user:[email protected]/db_name.

Both those methods will automatically start a new connection, regardless of whether or not you already have or will start a mongoose connection.

In the latest code, you can pass in a handle to an existing connection, an instance of mongodb.Db. With mongoose, this would be mongoose.connection.db. However, this code isn't in an actual release, and when I tried it, it didn't work. Probably not ready to be used yet (or untested).

I'm sure if you wait for the next release, you'll be able to pass in an existing mongoose connection. In the mean time you'll just need to accept having two connections, one from mongoose and one from connect-mongodb.

I got the connection info from https://github.com/tedeh/connect-mongodb and I got the handle information from viewing the source (relevant commit).

0

I just stumble across mongoose-session

Very lightweight and worked seamlessly for me. From github...

Installation

npm install mongoose-session

Use

var express = require('express');

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');

var app = express();

app.use(require('express-session')({
    key: 'session',
    secret: 'SUPER SECRET SECRET',
    store: require('mongoose-session')(mongoose)
}));
1
  • 2
    From repo mongoose-session, INACTIVE. Use connect-mongo instead. It's better anyways.
    – David
    Jul 31, 2016 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.