如何使用app.method()来进行授权

我正在尝试使用快速然后next('route')但我没有在如何做到这一点的文件中find很多, 这里提到。 但是并没有解释如何去做

将只在通过使用app.METHOD()或router.METHOD()函数加载的中间件函数中起作用。

我没有得到他们的意思,因为我从来没有用过app.METHOD()之前,无法find关于如何正确使用next('route')文档。 这会工作吗?

 app.method('*', (req, res, next) => { if(req.user == null) { next(); } else { User.findOne({"_id": req.user.id}, function(err, result) { if(err){ console.log(err); } else { if(stripe.active == false && stripe.trial == false){ res.render('/dashboard/billing'); next('route'); } else { next(); } } }); } }); 

这甚至会工作吗? 我猜在那里我搞砸了一些东西。 如果我这样做,那么它会find一个用户,然后检查是is_activeis_trial是假的,如果是这样跳到下一个路线。 我这样做的想法是,使用该网站的任何部分没有login的用户,那么当有login用户,都是假的,我只让他们去/dashboard/billing 。 我build议这样做,以防止尚未付款的用户访问我的应用程序。

我正在testing的路线:

 // dashboard app.get('/dashboard', setRender('dashboard/index'), setRedirect({auth: '/login'}), isAuthenticated, (req, res, next) => { }, dashboard.getDefault); 

首先,将方法更改为正确的HTTP方法请求。 在文档中,它指定了.METHOD()表示的内容:

app.METHOD()函数,其中METHOD是中间件函数处理的请求的HTTP方法(例如GET,PUT或POST)。

换句话说,.METHOD()是.get,.post,.put等的占位符

尝试改变你的应用程序:

 app.get('*', (req, res, next) => { if(req.user == null) { next(); } else { User.findOne({"_id": req.user.id}, function(err, result) { if(err){ console.log(err); } else { if(stripe.active == false && stripe.trial == false){ res.render('/dashboard/billing'); next('route'); } else { next(); } } }); } }); 

看看你能不能从那里解决。

更新

在res.render()之后调用.next('route')会取消res.render(),因为.next('route')会将请求发送到下一个路由器。