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
-
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 callapp.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
1 Answer
Reset to default 3Decided 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
-
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 callapp.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
1 Answer
Reset to default 3Decided 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
版权声明:本文标题:javascript - req.session undefined in Express.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745580052a2157255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论