I'm trying to use the sessions in Express. I've read the docs as carefully as I could but couldn't find any differences between my code and the examples - still, sessions doesn't seem to initialize.
Here's the start of my app.js
var express = require('express')
, routes = require('./routes')
, customRoutes = require('./routes/custom.js');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
req.session.something = "please?";
req.session.boo = true;
req.session.int = 100;
console.log(req.session); // undefined
});
And when I run it: TypeError: Cannot set property 'something' of undefined.
What's to check?
$ npm ls
ââ⬠cradle@0.5.7
â âââ vargs@0.1.0
â ââ⬠vows@0.5.13
â âââ eyes@0.1.6
ââ⬠express@2.5.0
â âââ connect@1.7.2
â âââ mime@1.2.4
â âââ mkdirp@0.0.7
â âââ qs@0.3.2
âââ node-static@0.5.9
You didn't specify any Session store, check the example from here: https://github.com/alessioalex/Nodetuts/blob/master/express_samples/app.js#L15-31
For example specify the memory store (this is ok only for development):
var MemStore = express.session.MemoryStore;
...
app.use(express.session({secret: 'secret_key', store: MemStore({
reapInterval: 60000 * 10
})}));