比如在Meteor中,有这样的东西
Router.plugin('ensureSignedIn');
Router.plugin('ensureSignedIn', {
except: ['home', 'atSignIn', 'atSignUp', 'atForgotPassword']
});
因此未签名的用户无法访问除以上四种之外的其他路由。
如何在 express.js 中做到这一点?我也在使用 passport.js。
最佳答案
我不熟悉 Meteor,但是你可以做类似下面的事情,假设你想让页面只对经过身份验证的用户( Passport )可用。
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated())
return next();
else
// Return error content: res.jsonp(...) or redirect: res.redirect('/login')
}
app.get('/account', ensureAuthenticated, function(req, res) {
// Do something with user via req.user
});
ensureAuthenticated
函数只是一个示例,您可以定义自己的函数。调用 next()
继续请求链。
关于node.js - 如何保护 express.js 中的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615688/