可能重复:
‘session’ is undefined when using express / redis for session store
var express = require('express');
var app = express();
var RedisStore = require('connect-redis')(express);
var session = require('redis-session')({
debug: true,
ttl: 90000000,
connection: {port: '7170', host: '127.0.0.1'}
});
app.configure(function(){
app.use(express.bodyParser());
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));
app.use(express.cookieParser());
app.use(app.router);
});
使用此配置,只要我调用req.session
,nodemon就会告诉我它未定义。我想,express.cookieParser()
和express.session
已经很好地宣布了。任何解决方案......?
答案 0 :(得分:3)
cookieParser
中间件需要在session
中间件之前,所以交换顺序如下:
app.use(express.cookieParser());
app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));
请参阅此here上的Connect文档。