我有一个nodejs-express app正在运行,并且是新手。
在我的主页('/')中,我有以下内容:
app.get('/', function(req, res){
// check if the user's credentials are saved in a cookie //
if (req.cookies.user == undefined || req.cookies.pass == undefined){
res.render('login', { title: 'KrishiMoney' });
} else{
// attempt automatic login //
AM.autoLogin(req.cookies.user, req.cookies.pass, function(o){
if (o != null){
req.session.user = o;
res.redirect('/home');
} else{
res.render('login', { title: 'KrishiMoney' });
}
});
}
});
现在当我打印req.cookies.user时,它给了我username.Anyhow,somwhere沿着我有管理路线的路线:
app.get('/admin', function(req, res){
var admin_username = 'admin'
var admin_password = 'nansat'
console.log(req.cookies.user);
//if (req.session.user != admin_username && req.session.pass != admin_password) res.redirect('404');
(res.render('admin_login', {title : 'Admin Page'}));
});
这里当我输入相同的命令req.cookies.user时,它给了我undefined。我如何保留用户并通过路线?