admin管理员组

文章数量:1022989

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.

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.

本文标签: javascriptreqsession undefined in ExpressjsStack Overflow