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

4
  • 1
    Did you try moving stuff around, like instantiating the session before the routes, and did you set session.user somewhere ? Commented Dec 23, 2013 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. Commented Dec 23, 2013 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 ! Commented Dec 23, 2013 at 23:54
  • Sure, added an answer Commented Dec 24, 2013 at 0:32

1 Answer 1

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.