是否可以在ExpressJS中禁用/删除特定路由的中间件? [英] Is it possible to disable/remove a middleware for specific route in ExpressJS?
本文介绍了是否可以在ExpressJS中禁用/删除特定路由的中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想禁用以前在app.js中设置的特定中间件,例如:
I want to disable a specific middleware, which I've set up previously in the app.js, for example:
app.use(express.bodyParser());
然后我想删除那个 bodyParser()例如:
And then I want to remove that bodyParser() for a specific route for instance:
app.post("/posts/add", Post.addPost);
谢谢
推荐答案
您可以编写一个函数来检测条件,如下所示:
You could write a function to detect a condition, like this:
function maybe(fn) {
return function(req, res, next) {
if (req.path === '/posts/add' && req.method === 'POST') {
next();
} else {
fn(req, res, next);
}
}
}
然后修改应用程序。使用语句:
And then modify the app.use statement:
app.use(maybe(express.bodyParser()));
这篇关于是否可以在ExpressJS中禁用/删除特定路由的中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文