'Nestjs Middlware in main.ts get request body

When I'm getting request from frontend, I put log in route handler for example

@Post('create')
createPost(
@Body() body: CreatePostDto,
){
  console.log(body); // I'm getting body correctly
}

But I want to add bodyParser in main.ts because of parse formData which is all string and I want to get other types which is added from front. So I checked that when I put middleware in main.ts

app.use((req, res, next) => {
console.log(req.body) //its empty object so why?
})

So I have 2 questions.

  1. Why in middleware I get empty body?
  2. How to parse formdata?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source