0

Possible Duplicate:
‘session’ is undefined when using express / redis for session store

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

  debug: true,
  ttl: 90000000,
  connection: {port: '7170', host: '127.0.0.1'}    

});

app.configure(function(){

   app.use(express.bodyParser());
   app.set('views',__dirname + '/views');
   app.set('view engine', 'ejs');
   app.use(express.static(__dirname + '/public'));
   app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));
   app.use(express.cookieParser());
   app.use(app.router);

});

With this configuration, any time I call req.session, nodemon tells me that it's undefined. express.cookieParser() and express.session are well declared, I guess. Any solution...?

Community
  • 1
  • 1
MrMangado
  • 993
  • 4
  • 10
  • 26
  • 1
    Man, the nazis on this site marking things as duplicates or "not fitting the feel of the site" need to probably make sure they know what they are talking about. This question is not a duplicate because it uses the Redis store for sessions and has a completely different problem. It's a pity. – electrichead May 13 '13 at 20:32

1 Answers1

3

The cookieParser middleware needs to come before the session middleware so swap the order like this:

   app.use(express.cookieParser());
   app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));

See the Connect docs on this here.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • I changed that, but it didn't work – MrMangado Dec 22 '12 at 16:47
  • 4
    Why did you pick this answer as correct when it didn't work for you? I have the exact issue and there probably would have been a correct answer here if the question were still open. – electrichead May 13 '13 at 20:33
  • This is simple but you also need to have redis running on your machine. In another terminal window run `redis-server`. – mhkeller Nov 09 '14 at 17:05