Skn0tt / nextjs-nestjs-integration-example

https://nextjs-nestjs-integration-example.now.sh
156 stars 36 forks source link

NestJS can't receive data #2

Closed hoangnq3004 closed 4 years ago

hoangnq3004 commented 4 years ago

Hi @Skn0tt !

I want post data to http://localhost:3000/api/randomNumber with 'Content-Type': 'application/json' but NestJS controller can't receiva data.

app.controller.ts

    @Post()
    randomNumber(@Body() postDTO) {
        console.log(postDTO)
        return Math.random() * 100;
    }

.babelrc

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ],
    "babel-plugin-parameter-decorator"
  ]
}

When remove 'Content-Type', NestJS cant receive Body data, but I need JSON.parse it!

Skn0tt commented 4 years ago

Hi @hoangnq3004, the provided information is not sufficient for me to efficiently help you. I can have a look if you could provide me with a minimal reproduction case (as in here).

Skn0tt commented 4 years ago

You can have a look at your bootstrap-Function though, any body-parser instance that deals with application/json can cause your project to hang.

hoangnq3004 commented 4 years ago

Sorry for my information!

You can see at https://github.com/hoangnq3004/nextjs-nestjs-integration-example

Skn0tt commented 4 years ago

Since Next.js already parsed the payloads, the body-parser instance that NestJS enables by default will be passed a non-string body. This causes it to hang.

You'll need to disable body-parser:

const app = await NestFactory.create(AppModule, { bodyParser: false });
Skn0tt commented 4 years ago

I've extended the blog post to cover this.

hoangnq3004 commented 4 years ago

Thank you!