How to store session values with Node.js and mongodb?

Posted by Tirithen on Stack Overflow See other posts from Stack Overflow or by Tirithen
Published on 2011-03-19T19:09:01Z Indexed on 2011/03/20 8:10 UTC
Read the original article Hit count: 263

Filed under:
|
|
|

How do I get sessions working with Node.js, [email protected] and mongodb? I'm now trying to use connect-mongo like this:

var config = require('../config'),
    express = require('express'),
    MongoStore = require('connect-mongo'),
    server = express.createServer();

server.configure(function() {
    server.use(express.logger());
    server.use(express.methodOverride());
    server.use(express.static(config.staticPath));
    server.use(express.bodyParser());
    server.use(express.cookieParser());
    server.use(express.session({
        store: new MongoStore({
            db: config.db
        }),
        secret: config.salt
    }));
});

server.configure('development', function() {
    server.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});

server.configure('production', function() {
    server.use(express.errorHandler());
});

server.set('views', __dirname + '/../views');
server.set('view engine', 'jade');

server.listen(config.port);

I'm then, in a server.get callback trying to use

req.session.test = 'hello';

to store that value in the session, but it's not stored between the requests.

It probobly takes something more that this to store session values, how? Is there a better documented module than connect-mongo?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about session