'session' is undefined when using express / redis for session store
Asked Answered
D

5

21

I'm trying to use redis for sessions in my express app.

I do the following:

var express = require('express');
var RedisStore = require('connect-redis')(express);

app.configure('development', function(){     
    app.use(express.session({ secret: "password", 
                            store: new RedisStore({
                                          host: "127.0.0.1",
                                          port: "6379",
                                          db: "mydb"
                                        })  
          }));

Later on, in my app, if i do something like:

var whatever = req.session.someProperty;

I get:

Cannot read property 'someProperty' of undefined

This indicates that req.session is undefined (I can see this from a console.log entry in my config section)

I've definitely got redis running, and can see my app connects to it initially (using redis-cli monitor)

Defazio answered 17/4, 2012 at 12:54 Comment(0)
R
10

Looks like you're missing:

app.use(express.cookieParser());

before your app.use(express.session(...)); call.

See here.

Roseliaroselin answered 17/4, 2012 at 13:1 Comment(1)
I'm having the same problem, but I do have app.user(cookieParser()) thingNitz
G
53

Sessions won't work unless you have these 3 in this order:

app.use(express.cookieParser());
app.use(express.session());
app.use(app.router);

I'm not sure if router is mandatory to use sessions, but it breaks them if it's placed before them.

Graeco answered 20/4, 2012 at 1:43 Comment(8)
I had app.use(app.router); before express.session, which seems to break it.Anadromous
this answer was hard to find, should be somewhere in the docs. Upvoted!Topical
This answer is more complete. You need to do both things: 1) enable cookieParser() and 2) move 'app.use(app.router);' to the bottom. I think this should be the correct answer.Jannery
even with all 3 things in right order,req.session still undefined, do not know why.Allan
Sir, I wish i could give you +2. You solved a TON of my problemsPharmacology
Cheers.. !! Feeling nice to fix this.Swine
How would this answer update since version 1.5.0? Currently, express-session is a separate module. According to the docs, "the cookie-parser middleware no longer needs to be used for this module to work"Cinereous
I'm afraid I have to let someone else answer that. I abandoned node 5 years ago.Graeco
R
10

Looks like you're missing:

app.use(express.cookieParser());

before your app.use(express.session(...)); call.

See here.

Roseliaroselin answered 17/4, 2012 at 13:1 Comment(1)
I'm having the same problem, but I do have app.user(cookieParser()) thingNitz
S
4

Had the same problem, however it was caused by changes in the latest version of express.

You now need to pass express-session to the function connect-redis exports to extend session.Store:

var express = require('express');
var session = require('express-session')
var RedisStore = require('connect-redis')(session);
Solvency answered 8/3, 2014 at 11:6 Comment(0)
L
4

Things have changed recently with Express 3 / Express 4. Please confirm you are using version 4.

The complete middleware concept changed. You need to install these middlewares manually. "express-session" is one of the 4.0 middlewares.

I recommend to read

http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0 and https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x

Additionally some users were confused that the github repo itself is named just "session" but

npm install express-session

is correct.

Lindgren answered 11/4, 2014 at 9:34 Comment(2)
This question was asked Apr 17 2012, therefore NOT related to version 4Defazio
For version 4: #31612420Dillydally
A
2

I had the same problem. It turned out that redis was simply configured to a different port.

Agonized answered 30/9, 2013 at 21:41 Comment(1)
Thanks for putting me on the right track. I tripped over the fact that Heroku changed the REDIS_URL that I was using on my localhost. I could have known this as they mention this themselves: > "the REDIS config vars may change at any time. Relying on the config var outside of your Heroku app may result in you having to re-copy the value if it changes" devcenter.heroku.com/articles/…Transpadane

© 2022 - 2024 — McMap. All rights reserved.