0

我尝试在 AppFog 上部署我的 NodeJS 应用程序,但我遇到了一些问题......我使用 express 的会话并且我的会话不起作用,但是,它在本地工作,但在 AppFog 上不起作用。

我的错误:

    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)

我的用户控制器:

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('/');
}

我的 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'))
        });
    }
});
4

1 回答 1

1

您应该在此处提供密钥。

app.use(express.cookieParser('lysarmony'));
app.use(express.session({secret: 'some key'}));
于 2013-10-11T11:38:01.047 回答