Kemical
Kemical

Reputation: 7

req.session undefined in Express.js

I've seen several posts related to this but none solved my problem.

I have this code in server.js

var express = require('express');
var app = express();
app.configure(function(){
    app.set(express.cookieParser());
        app.set(express.session({secret: "This is a secret"}));
});
app.get('/name/:name', function(req, res){
    req.session.name = req.params.name;
    res.send("<a href='/name'>GO</a>");
});
app.get('/name', function(req, res){
    res.send(req.session.name);
});
app.listen(3000);

When I go to http://localhost:3000/user/someone that's the output that I get TypeError: Cannot set property 'name' of undefined at /Users/Me/Node/server.js:10:19 at callbacks

Upvotes: 0

Views: 8031

Answers (1)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143061

Decided to copy from comments. Try replacing

app.configure(function(){
    app.set(express.cookieParser());
        app.set(express.session({secret: "This is a secret"}));
});

with

app.use(express.cookieParser());
app.use(express.session({secret: "This is a secret"}));

and see what happens.

Upvotes: 3

Related Questions