2

I've gone through many questions with the same issue, but none of the various solutions have helped. I'm using Redis to store session in a clustered NodeJS+ExpressJS application, but the session is always undefined. Here's my Express setup:

var express = require('express'),
    RedisStore = require('connect-redis')(express),
    Config = require('./config/config'),
    cluster = require("cluster"),
    QueryManager = require('./service/query_manager'),
    app = express();



// --- Index --- //
function renderSplash(req, res) {
    res.render(...);
}
function renderIndex(req, res) {
    res.render(...);
}

app.get('/', function(req, res) {
    if(req.session.user === null) {
        renderSplash(req, res);
    } else {
        renderIndex(req, res);
    }
});

// --- Configuration ---//
//EJS
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');
app.set('views', __dirname + '/public');

app.configure(function() {
    //Session
    app.use(express.cookieParser());
    app.use(express.session({
        store: new RedisStore({
            host: Config.redis.host,
            port: Config.redis.port
        }),
        secret: 'Its a secret.',
        cookie: { secure: true }
    }));    

    app.use(validateRequest); //Ensures we're at www. to hit the LB
    app.use(express.static(__dirname+'/public'));
    app.use(express.compress);
    app.use(app.router);
});

Even without using the Redis store, I'm getting the following error: TypeError: Cannot read property 'user' of undefined

kz3
  • 785
  • 2
  • 10
  • 23
  • 1
    Did you try moving stuff around, like instantiating the session before the routes, and did you set session.user somewhere ? – adeneo Dec 23 '13 at 23:49
  • I've tried moving the configurations all over, with no success. And doing something like `req.session.user = "Hi";` throws the same error, because `req.session` is undefined. – kz3 Dec 23 '13 at 23:51
  • @adeneo Whoops, I read your comment too quick. I hadn't tried configuring the sessions before the routes, that did the trick. If you write that as the answer I'll accept. Thanks ! – kz3 Dec 23 '13 at 23:54
  • Sure, added an answer – adeneo Dec 24 '13 at 00:32

1 Answers1

2

You'll have to instantiate the sessions before the routes.

var express = require('express'),
    RedisStore = require('connect-redis')(express),
    Config = require('./config/config'),
    cluster = require("cluster"),
    QueryManager = require('./service/query_manager'),
    app = express();

app.use(express.cookieParser());
app.use(express.session({
    store: new RedisStore({
        host: Config.redis.host,
        port: Config.redis.port
    }),
    secret: 'Its a secret.',
    cookie: { secure: true }
}));    

// --- Index --- //
function renderSplash(req, res) {
    res.render(...);
}
function renderIndex(req, res) {
    res.render(...);
}

app.get('/', function(req, res) {
    if(req.session.user === null) {
        renderSplash(req, res);
    } else {
        renderIndex(req, res);
    }
});
adeneo
  • 312,895
  • 29
  • 395
  • 388