req.session设置值后,在另外一个文件获取不到req.session中的值
(app.js)session参数:
app.use(session({
key:'keyboard',
cookie:{maxAge:3000},
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true ,cookie: { maxAge: 60000 }},
store: new MongoStore({
url:config.mongodb_url,
autoRemove: 'interval',
autoRemoveInterval: 1,
ttl: 60
})
}));
(./routers/user)用户登录:
router.post('/login',function(req,res,next){
var userAccount = req.body.user_account;
var login_password = req.body.login_password;
var userInfo = req.session.userInfo;
if (userAccount=="admin"&&login_password=="admin") {
userInfo['admin']={'userName':'admin'};
res.redirect("/manage/blacklists");
}else{
req.flash('info','登录失败');
res.redirect('/user/login');
};
});
(./routers/manage)个人中心:
router.get('/blacklists',function(req,res,next){
console.log(req.session);
if (!req.session) {
BlackList.find({},function(err,docs){
if (err) {return;};
res.render('manage',{'blackLists':docs});
});
}else{
res.redirect('/user/login');
};
});
在登录成功后设置req.session.userInfo的值,然后重定向到个人中心,获取不到req.session里面的值,像一个新的会话一样。 请问是什么问题,谢谢你的回答!