本文介绍了通过ExpressJS在AppFog上进行的会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ExpressJS App上的会话遇到问题.在本地,它可以正常运行,但是当我在AppFog上托管我的App时,遇到一些会话问题,有时会丢失...在我的管理面板中.
I get a problem with sessions on my ExpressJS App. In local it works perfectly but when I host my App on AppFog I get some issues with the sessions, they are lost sometimes... In my admin panel.
我不知道为什么,但是,刷新页面后,会话将不再起作用...
I don't know why but, when the page is refreshed, the sessions no longer to work...
var restrictedArea = function(req, res, next) {
if( req.session.access )
next();
else
res.redirect('/signin');
}
// Get/Post example on admin panel
app.get('/posts/add', restrictedArea, admin.addPost);
app.post('/posts/add', restrictedArea, admin.savePost);
app.post('/posts/delete', restrictedArea, admin.deletePost);
app.get('/posts/edit/:id', restrictedArea, admin.editPost);
我的app.configure:
My app.configure:
app.configure(function () {
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.static(path.join(__dirname, 'public')));
//app.use(express.session({secret: 'some secret'}));
app.use(express.cookieSession({
secret: 'SECRET',
cookie: { access: false }
})
);
app.use(express.session({secret: 'some secret'}));
app.use(app.router);
app.use(function(req, res, next){
res.render('404.jade', {
title: "404 - Page Not Found",
showFullNav: false,
status: 404,
url: req.url
});
});
});
我的路线:
exports.authenticate = function(req, res) {
if( req.body.username == user.username && req.body.password == user.password ){
req.session.access = true;
renderToAdminIndex(req, res);
}else{
req.session.access = false;
renderToSignin(req, res, "Error, please try again!");
}
}
退出功能:
exports.signout = function(req, res) {
req.session.destroy(function(){
res.redirect('/');
});
}
安东尼
推荐答案
如果您使用Express 3.x,则需要使用express.cookieSession
:
If your using express 3.x you need to use express.cookieSession
:
app.configure(function () {
...
app.use(express.cookieParser());
app.use(express.cookieSession(
{
secret: 'SECRET',
cookie: { maxAge: ... }
}
));
参考: http://expressjs.com/api.html#cookieSession
更新
要擦除会话,请执行以下操作:
To wipeout the session:
exports.signout = function(req, res) {
req.session = null;
res.redirect('/');
}
这篇关于通过ExpressJS在AppFog上进行的会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!