node.jsexpressappfog

Session with NodeJS on AppFog


I try to deploy my NodeJS app on AppFog but I get some issues... I used the sessions with express and my sessions doesn't work, however, it works in local but not on AppFog.

My error :

    500 TypeError: Cannot call method 'indexOf' of undefined
    at ServerResponse.res.location (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/response.js:637:13)
    at ServerResponse.res.redirect (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/response.js:694:8)
    at exports.authenticate (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/controllers/users.js:11:7)
    at callbacks (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/router/index.js:161:37)
    at param (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/router/index.js:135:11)
    at pass (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/router/index.js:142:5)
    at Router._dispatch (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/router/index.js:170:5)
    at Object.router (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/lib/router/index.js:33:10)
    at next (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at store.get.next (/mnt/var/vcap.local/dea/apps/lysarmony-0-3fd87f0ba62aaba69653e3cfc032b903/app/node_modules/express/node_modules/connect/lib/middleware/session.js:313:9)

My users controller :

exports.signin = function(req, res) {
    res.render('users/signin');
}

exports.authenticate = function(req, res) {
    var username = req.body.username;
    var password = req.body.password;
    if( username == '****' && password == '*****' ){
        req.session.access = true;
        res.redirect(req.session.url);
    }else{
        res.render('users/signin', {
            message: 'Error, please try again !'
        });
    }
}

exports.signout = function(req, res) {
    req.session = null;
    res.redirect('/');
}

My app.js :

/**
 * Module dependencies.
 */

var express = require('express')
    , index = require('./controllers')
    , users = require('./controllers/users')
    , creations = require('./controllers/creations')
    , http = require('http')
    , path = require('path')
    , db = require('./models');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('lysarmony'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.set('__report', './public/reports/');
app.use('/bower_components', express.static(__dirname + '/bower_components'));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

// Authentication function
function requireAuthentication(req, res, next){
    if( req.session.access ){
        next();
    }else{
        req.session.url = req.url;
        res.redirect('/signin');
    }
}

app.get('/signin', users.signin);
app.post('/signin', users.authenticate);
app.get('/signout', users.signout);

app.get('/glovebox', requireAuthentication, creations._index);



db.sequelize.sync().complete(function(err) {
    if (err) {
        throw err
    } else {
        http.createServer(app).listen(app.get('port'), function(){
            console.log('Express server listening on port ' + app.get('port'))
        });
    }
});

Solution

  • You should provide the secrete key here.

    app.use(express.cookieParser('lysarmony'));
    app.use(express.session({secret: 'some key'}));