how to use sessions in express, couchDB, and node.js
Solution 1
If 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.
Solution 2
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.
Related videos on Youtube
Ernesto11
Updated on July 15, 2020Comments
-
Ernesto11 almost 4 years
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!
-
twiz about 11 yearsThe
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.
-
-
Ernesto11 about 13 yearsI tried that and it's still not working. Why would I have to initialize my server like that? it says in the express documentation to use
-
Ernesto11 about 13 yearsapp.use(express.cookieParser()); app.use(express.session({ secret: "keyboard cat" }));
-
marc_d about 13 yearsBoth are valid ways to initialize middleware with express.js. The only difference for me is that sessions only work if I initialize them in app.createServer. Might be a bug.
-
Ernesto11 about 13 yearsOk yeah. Everything works now. Idk why it wasn't earlier. Thanks a lot guys!
-
RobKohr over 12 yearsJust joining the others who have stumbled upon your answer when need of help... thanks!
-
nak over 12 years@Ernesto11 you should give this a green check :) It worked for me as well
-
CpILL about 12 yearsno work for me. Only adding it to the express.createServer() call seems to make it happen :-/
-
Yuri Subach almost 12 yearsWorks for me, thank you. The main idea IMHO is to 'use()' cookieParser and session before all other 'uses'. My code:
var app = express.createServer(); app.use(express.cookieParser()); app.use(express.session({ secret: 'secret key' })); app.use(express.bodyParser());
-
nax almost 12 yearsJust to clarify, I had the same problem but this doesn't fix that at all. But I discovered you have to put the cookieParser, session and router at the end of the configuration in that order. hope someone more helps!
-
Farzher over 11 yearsYou need
app.use(express.cookieParser());
beforeapp.use(express.session({ secret: '}{91
/,48+F{tf2'}));`