javascript - req.session undefined in Express.js - Stack Overflow

I've seen several posts related to this but none solved my problem.I have this code in server.jsva

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

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

Share Improve this question asked Oct 26, 2013 at 15:21 KemicalKemical 71 silver badge5 bronze badges 4
  • 1 wasn't that app.use(express.cookieParser()), etc? – Michael Krelin - hacker Commented Oct 26, 2013 at 15:29
  • 1 (also, I think app.configure is not needed and only kept as legacy, you should just call app.use(stuff)). – Michael Krelin - hacker Commented Oct 26, 2013 at 15:31
  • The app.configure() method is not legacy, and neither is it used incorrectly here. The issue here is that the person who asked the question is using the method used to set settings rather than the method to apply middleware. – hexacyanide Commented Oct 26, 2013 at 19:06
  • The docs seem to suggest it's legacy... – robertklep Commented Oct 26, 2013 at 19:21
Add a ment  | 

1 Answer 1

Reset to default 3

Decided to copy from ments. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580054a4634200.html

相关推荐

  • javascript - req.session undefined in Express.js - Stack Overflow

    I've seen several posts related to this but none solved my problem.I have this code in server.jsva

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信