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?
Here, since sess is global, the session won't work for multiple users as the server will create the same session for all the users. This can be solved by using what is called a session store. We have to store every session in the store so that each one will belong to only a single user.
You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost .
Yes, we're actually using multiple drivers, in a production application. We need connections to multiple databases, and mongoose is only able to connect to one DB. So we use MongoDB for the connections to the secondary databases. It should be the same using MongoJS instead.
in the end i'm using a bit of every answer that was given before:
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); });
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
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