Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...