2013-10-11 62 views
0

我嘗試在AppFog上部署NodeJS應用程序,但我遇到了一些問題......我使用了Express會話,但會話不起作用,但是它在本地但不在AppFog上工作。與AppFog上的NodeJS會話

我的錯誤:

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

回答

1

你應該在這裏提供的私有密鑰。

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

好的,謝謝你,它的工作原理。 – tonymx227