visionmedia/express — Issue #913: (Express 2.5.1) Sessions break if order within configure is altered
Author: beseku
Created: 2011-12-02 06:10:16+00:00
State: open

The order of items executed in the app.configure method call of a boiler plate app will affect whether the session variable is available or undefined. Steps to reproduce are below:

Create an application with sessions included:

express foo --sessions
cd foo
npm install

Update the default root to log the session variable:

exports.index = function(req, res){
console.log(req.session)
res.render('index', { title: 'Express' })
};

Run the server to output the session details as they should be. Results in the following written to the console:

Express server listening on port 3000 in development mode
{ lastAccess: 1322806070016,
cookie:
{ path: '/',
httpOnly: true,
_expires: Fri, 02 Dec 2011 10:07:50 GMT,
originalMaxAge: 14400000 } }

Move the `app.use(app.router); ` call in the app.configure call above the session and cookieParser calls:

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(app.router);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(express.static(__dirname + '/public'));
});

Re-run the server to output the broken session details. Results in the following written to the console:

Express server listening on port 3000 in development mode
undefined