admin管理员组文章数量:1026989
so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.
I am using Node.js,express,and couchDB.
Here is how i set up my session so far
var MemoryStore = require('connect').session.MemoryStore;
app.use(express.cookieParser());
app.use(express.session({
secret: "keyboard cat",
store: new MemoryStore({
reapInterval: 60000 * 10
})
}));
To store something in the session, i use the following code right?
req.session = {user:name);
So the session variable seems to work on my login page. I successfully store the user's name into the session However, when I try to access the session variable on another page, it gives me the error
Cannot read property 'user' of undefined
all i'm doing is:
if (req.session.user){
Why is this error happening? are sessions not global to the whole app? Or am I missing something entirely here.
Thanks in advance!
so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.
I am using Node.js,express,and couchDB.
Here is how i set up my session so far
var MemoryStore = require('connect').session.MemoryStore;
app.use(express.cookieParser());
app.use(express.session({
secret: "keyboard cat",
store: new MemoryStore({
reapInterval: 60000 * 10
})
}));
To store something in the session, i use the following code right?
req.session = {user:name);
So the session variable seems to work on my login page. I successfully store the user's name into the session However, when I try to access the session variable on another page, it gives me the error
Cannot read property 'user' of undefined
all i'm doing is:
if (req.session.user){
Why is this error happening? are sessions not global to the whole app? Or am I missing something entirely here.
Thanks in advance!
Share Improve this question edited Feb 16, 2013 at 4:21 Yasin Okumuş 2,3897 gold badges33 silver badges65 bronze badges asked Apr 8, 2011 at 18:09 Ernesto11Ernesto11 2811 gold badge3 silver badges6 bronze badges 1 |4 Answers
Reset to default 71If you're doing app.use(app.router)
before the lines that set up the cookie and session handling, try moving it to after them. That fixed the same problem for me.
I was trying to solve the exact same problem for he last few hour.
Try initializing your server like that:
var app = express.createServer(
express.cookieParser(),
express.session({ secret: "crazysecretstuff"})
);
That should work.
I had same issue of getting undefined for session variable which I knew was set ok.
In my case it turned out to be caused by cross origin request, I had forgotten the withCredentials header on the request.
So for example in my client side Angular app I needed to do this:
var config = { withCredentials: true };
return $http.get(appConfig.apiUrl + '/orders', config);
and in Express this:
res.header('Access-Control-Allow-Credentials', true);
the sessionSecret
in the configuration (config.json?) should be non-empty:
sessionSecret: "something other than an empty string",
so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.
I am using Node.js,express,and couchDB.
Here is how i set up my session so far
var MemoryStore = require('connect').session.MemoryStore;
app.use(express.cookieParser());
app.use(express.session({
secret: "keyboard cat",
store: new MemoryStore({
reapInterval: 60000 * 10
})
}));
To store something in the session, i use the following code right?
req.session = {user:name);
So the session variable seems to work on my login page. I successfully store the user's name into the session However, when I try to access the session variable on another page, it gives me the error
Cannot read property 'user' of undefined
all i'm doing is:
if (req.session.user){
Why is this error happening? are sessions not global to the whole app? Or am I missing something entirely here.
Thanks in advance!
so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.
I am using Node.js,express,and couchDB.
Here is how i set up my session so far
var MemoryStore = require('connect').session.MemoryStore;
app.use(express.cookieParser());
app.use(express.session({
secret: "keyboard cat",
store: new MemoryStore({
reapInterval: 60000 * 10
})
}));
To store something in the session, i use the following code right?
req.session = {user:name);
So the session variable seems to work on my login page. I successfully store the user's name into the session However, when I try to access the session variable on another page, it gives me the error
Cannot read property 'user' of undefined
all i'm doing is:
if (req.session.user){
Why is this error happening? are sessions not global to the whole app? Or am I missing something entirely here.
Thanks in advance!
Share Improve this question edited Feb 16, 2013 at 4:21 Yasin Okumuş 2,3897 gold badges33 silver badges65 bronze badges asked Apr 8, 2011 at 18:09 Ernesto11Ernesto11 2811 gold badge3 silver badges6 bronze badges 1-
2
The
req.session = {user:name);
part of your code is no longer correct. I assume this worked in an older version of express, but now to avoid an error, you will need to usereq.session.user=name;
. If you don't, methods on req.session will be overwritten. – twiz Commented Mar 16, 2013 at 21:42
4 Answers
Reset to default 71If you're doing app.use(app.router)
before the lines that set up the cookie and session handling, try moving it to after them. That fixed the same problem for me.
I was trying to solve the exact same problem for he last few hour.
Try initializing your server like that:
var app = express.createServer(
express.cookieParser(),
express.session({ secret: "crazysecretstuff"})
);
That should work.
I had same issue of getting undefined for session variable which I knew was set ok.
In my case it turned out to be caused by cross origin request, I had forgotten the withCredentials header on the request.
So for example in my client side Angular app I needed to do this:
var config = { withCredentials: true };
return $http.get(appConfig.apiUrl + '/orders', config);
and in Express this:
res.header('Access-Control-Allow-Credentials', true);
the sessionSecret
in the configuration (config.json?) should be non-empty:
sessionSecret: "something other than an empty string",
本文标签: javascripthow to use sessions in expresscouchDBand nodejsStack Overflow
版权声明:本文标题:javascript - how to use sessions in express, couchDB, and node.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737279600a1453527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
req.session = {user:name);
part of your code is no longer correct. I assume this worked in an older version of express, but now to avoid an error, you will need to usereq.session.user=name;
. If you don't, methods on req.session will be overwritten. – twiz Commented Mar 16, 2013 at 21:42