对Express中的特定动词使用中间件 [英] Use middleware for specific verbs in Express
本文介绍了对Express中的特定动词使用中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个快速应用程序,可以在其中使用中间件 adminFilterMiddleware
I have an express application where I handle authorization using middleware, adminFilterMiddleware
module.exports = function(req, res, next){
if(res.locals.isAdmin){
next();
return;
}
res.status(403).send('403 Forbidden').end();
};
我将中间件应用于某些只能由管理员访问的路由:
I apply the middleware for certain routes which should only be accessible by admins:
app.use('/only-for-admin', adminFilterMiddleware);
但是,有什么方法可以将中间件仅应用于某些动词,例如 DELETE
或 PUT
?
However, is there a way I can apply the middleware only on certain verbs, such as DELETE
or PUT
?
推荐答案
您可以使用 app.put('/:id',adminFilterMiddleware)
,然后此中间件将适用于所有放置请求.希望对您有所帮助:)
you may use app.put('/:id',adminFilterMiddleware)
then this middleware will be applicable to all put requests.
hope it helps :)
这篇关于对Express中的特定动词使用中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文