我想禁用一个特定的中间件,这是我之前在app.js中设置的,例如:
app.use(express.bodyParser());
字符串然后我想删除特定路由的 bodyParser(),例如:
app.post("/posts/add", Post.addPost);
型谢谢你
vsdwdz231#
你可以写一个函数来检测一个条件,像这样:
function maybe(fn) { return function(req, res, next) { if (req.path === '/posts/add' && req.method === 'POST') { next(); } else { fn(req, res, next); } } }
字符串然后修改app.use语句:
app.use(maybe(express.bodyParser()));
型
bz4sfanl2#
在typescript中,你可以这样写一个except函数:
except
import { Request, Response, NextFunction, } from 'express'; type MiddlewareFn = (req: Request, res: Response, next: NextFunction) => void; export const middlewareExcept = (fn: MiddlewareFn, except: string[]): MiddlewareFn => (req, res, next) => { if (except.includes(req.path)) next(); else fn(req, res, next); };
个字符
2条答案
按热度按时间vsdwdz231#
你可以写一个函数来检测一个条件,像这样:
字符串
然后修改app.use语句:
型
bz4sfanl2#
在typescript中,你可以这样写一个
except
函数:个字符