nodejs routing:除第一页以外的所有具有认证的路由

fab*_*ous 1 routes node.js

我正在使用护照登录.我想要除了'/'要求登录之外的所有路线.

我有一个

ensureAuthenticated
Run Code Online (Sandbox Code Playgroud)

功能,检查我是否经过身份验证.

我宁愿不想把中间件调用放到每个路由中:

app.get('/first', ensureAuthenticated,...)
app.get('/second', ensureAuthenticated,...)
Run Code Online (Sandbox Code Playgroud)

有没有办法分配ensureAuthenticated给所有路线除外'/'

rob*_*lep 5

你可以用app.all它:

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

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