Node.js nodejs路由:除第一页外的所有具有身份验证的路由

Node.js nodejs路由:除第一页外的所有具有身份验证的路由,node.js,routes,Node.js,Routes,我正在使用passport登录。 我想要所有路由,除了需要登录的'/' 我有一个 ensureAuthenticated 函数,用于检查我是否经过身份验证 我不希望必须将中间件调用放在每条路由中: app.get('/first', ensureAuthenticated,...) app.get('/second', ensureAuthenticated,...) 是否有办法将确保重新验证的分配给除'/'以外的所有路线?您可以使用: // regular route app.get('/

我正在使用passport登录。 我想要所有路由,除了需要登录的
'/'

我有一个

ensureAuthenticated
函数,用于检查我是否经过身份验证

我不希望必须将中间件调用放在每条路由中:

app.get('/first', ensureAuthenticated,...)
app.get('/second', ensureAuthenticated,...)
是否有办法将
确保重新验证的
分配给除
'/'
以外的所有路线?

您可以使用:

// regular route
app.get('/', ...);

// make sure all following routes will be passed through ensureAuthenticated
app.all('*', ensureAuthenticated);
app.get('/first',  ...);
app.get('/second', ...);
您可以使用:

// regular route
app.get('/', ...);

// make sure all following routes will be passed through ensureAuthenticated
app.all('*', ensureAuthenticated);
app.get('/first',  ...);
app.get('/second', ...);

很好!感谢您提供了这个优雅的解决方案。注意:当然我也有一个app.post('/login')路线-这需要在app.all之前进行,这样我的登录才能正常工作:)效果很好!感谢您提供了这个优雅的解决方案。注意:当然我也有一个app.post(“/login”)路径-这需要在app.all之前进行,以便我的登录工作:)