Я (почти) успешно использую Node.js с Express и Redis для обработки сессий.
Проблема, с которой я сталкиваюсь, заключается в том, что сессия не сохраняется, когда я использую res.redirect()
.
Вот как я это вижу :
req.session.username = username.toString();
console.log(req.session);
res.redirect('/home');
В console.log() выводится :
{ lastAccess: 1322579131762,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 },
username: 'admin' }
Теперь, вот следующий код :
app.get('/home', [app.requireLogin], function(req, res, next) {
// Not showing the rest as it's not even getting there
// Instead, here is what's interesting
app.requireLogin = function(req, res, next) {
console.log(req.session);
В console.log() выводится вот это :
{ lastAccess: 1322579131775,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 } }
Очевидно, что объект 'username' исчез. Сессия не сохранила его, а просто перестроила новый.
Как я могу решить эту проблему? Не стесняйтесь, если вам нужна какая-либо информация.
Вот код, где я настраиваю управление сессиями :
app.configure(function() {
// Defines the view folder and engine used.
this.set('views', path.join(__dirname, 'views'));
this.set('view engine', 'ejs');
// Allow parsing form data
this.use(express.bodyParser());
// Allow parsing cookies from request headers
this.use(express.cookieParser());
// Session management
this.use(express.session({
// Private crypting key
secret: 'keyboard cat',
store: new RedisStore,
cookie: {
maxAge: 60000
}
}));
this.use(app.router);
});
Вот весь проект (в смысле, его части), на gist : https://gist.github.com/c8ed0f2cc858942c4c3b (игнорировать свойства визуализированных представлений)