Using Session MemoryStore in Express.js Does Not Define req.session

I installed a Node.js test server with Express.js, which looks like this:

var express     = require('express');
var MemoryStore = express.session.MemoryStore;
var app         = express.createServer();

app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(app.router);
    app.use(express.session({
        store: new MemoryStore(),
        secret: 'secret',
        key: 'bla'
    }));
});

app.get('/', function(req, res){
    console.log(req.session);
    res.end("html");
});

app.listen(3000);

Why console.log(req.session)prints undefined? Didn't I use it correctly? What do I need to do to save variables in a user session?

The client receives the SID, which is stored as its cookie, but I cannot assign any attributes req.sessionbecause it is not defined. Thanks for any tips to solve this problem! :-)

These are the versions I use:

├─┬ express@2.5.9
│ ├─┬ connect@1.8.7
│ │ └── formidable@1.0.9
│ ├── mime@1.2.4
│ ├── mkdirp@0.3.0
│ └── qs@0.4.2
+5
source share
2 answers

I don’t know what it is app.router, but this seems to be causing the problem. Removing this line made me work.

+2

: app.use(app.router) . , req.session. , , app.get ( app.post ..). , , app.use(app.router) .

. "" Configuration Middleware .

+20

All Articles