Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

node.js + express.js: session handling with mongodb/mongoose

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?

question from:https://stackoverflow.com/questions/6819911/node-js-express-js-session-handling-with-mongodb-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...