修改app.js文件,增加authentication,notAuthentication两个方法
function authentication(req, res, next) {
if (!req.session.user) {
req.session.error='请先登陆';
return res.redirect('/login');
}
next();
}
function notAuthentication(req, res, next) {
if (req.session.user) {
req.session.error='已登陆';
return res.redirect('/home');
}
next();
}配置好后,我们未登陆,直接访问localhost:3000/home时或者localhost:3000/logout,就会跳到/login页面
登录后, 访问localhost:3000/login 则直接跳到/home页面
到此,express4 相关内容到此为止。
以上内容是小编给大家分享的Nodejs Express4.x开发框架随手笔记,希望大家喜欢。










