var express = require('express');
var RedisStore = require('connect-redis')(express);
var app=express();
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: true
});
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
secret: 'keyboard cat',
store: new RedisStore()
}));
app.use(express['static'](__dirname + '/public'));
app.use(app.router);
});
require("./router")(app);
app.listen(process.env.PORT);
index.js
var index=function(req,res,next){
req.session.title="首页";
res.render('index',{
session:req.session
});
};
};
提示req.session.title="首页";TypeError: Cannot set property ‘title’ of undefined
不行,其实我们想问的是 req.session.xxx="yyyyy" 就会提示错误,就算不render也会出错,
像var RedisStore = require('connect-redis')(express);
你是怎么用的?这里
app.use(express.cookieParser());
app.use(express.session({
secret: 'keyboard cat',
store: new RedisStore()
}));
怎么设置?我都是按照网上那样复制上去的,结果总是出错
调整一下use的顺序试一下,我的也遇到过这种问题,我的是把app.use(app.router);放到session之前就会undefined,之后就好了,具体为什么我还没追代码,你可以试下把bodyParser、cookie、session放到最前面
看来,redis server没启动,不如用这个试看看:
app.use(express.cookieParser('your secret here'));
app.use(express.session());
我也遇见了同样的问题,并且把session调到后面了,可是typeof req.session的时候还是undefined app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.static(path.join(__dirname, ‘public’))); app.use(express.cookieParser(‘keyboard’)); app.use(express.session({ secret: 'blog’, key: 'blog’, store:new RedisStore(), cookie: {maxAge: 1000 * 60 * 60 * 24 * 30}//30 days })); app.use(app.router);
然后在一个方法里想设req.session.flag的时候设不进去,后面输出下面信息的时候是undefind,求帮助~~ console.log(typeof req.session);