express 中定义session 时,总说报未定义。
express 中定义session 时,总说报未定义。req.session.user = ‘123’;
//定义共享环境
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({secret:'poynt'}));
});
//定义开发环境
app.configure('development',function(){
app.use(express.static(__dirname+'/public'));
app.use(express.errorHandler({dumpExceptions:true, showStack:true}));
});
//定义生产环境
app.configure('production', function(){
app.use(express.static(__dirname+'/public', {maxAge:36000*24*365}));
app.use(express.errorHandler());
});
routers(app);
app.listen(8000);
5 回复
//定义共享环境app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({secret:'poynt'}));});
TypeError: Cannot set property ‘user’ of undefined
中间件use() 的顺序有问题:
app.use(express.methodOverride()); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({secret:‘poynt’}));}); app.use(app.router);
谢谢,第一次较正式的用express,呵呵。不太懂它的加载机制。
session
依赖 cookie
,所以cookieParser
必须在之前就要use
上。