禁用/覆盖bodyparser行为

时间:2019-05-24 07:38:34

标签: node.js express middleware body-parser

在将bodyparser注入作为中间件来表达应用程序之后,我们可以禁用或覆盖它吗?

在一个文件中(不可编辑,由框架完成) app.use(bodyParser.json()); 导出应用;

在其他文件中,我正在导入应用程序,在这里我想禁用其中一条路线的bodyParser。我有什么办法可以做到吗?

1 个答案:

答案 0 :(得分:0)

// export file
// ...

app.use((req, res, next) => {
  // check if the path need body parser or not
  if (BlackList.includes(req.path)) {
    bodyParser.json()
  }
  next()
});

// ...
// import file
// ...

// some router
BlackList.push('anyway')
app.get('/anyway', function (req, res, next) {
  // your code...
})

// ...
相关问题