Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] bodyParser configuration does not work #1052

Closed
jiayisheji opened this issue Sep 5, 2018 · 6 comments
Closed

[Bug] bodyParser configuration does not work #1052

jiayisheji opened this issue Sep 5, 2018 · 6 comments

Comments

@jiayisheji
Copy link

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I use NestApplicationOptions to turn on the bodyParser and cannot get the body

Expected behavior

I can get the body

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    bodyParser: true,
  });
  app.use((req, res, next) => {
    console.log('req.body', req.body);
    next();
  });
  await app.listen(3000);
}
bootstrap();

Print the result:

req.body undefined
import { NestFactory } from '@nestjs/core';
import { urlencoded, json } from 'body-parser';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(urlencoded({ extended: true }));
  app.use(json());
  app.use((req, res, next) => {
    console.log('req.body', req.body);
    next();
  });
  await app.listen(3000);
}
bootstrap();

Print the result:

req.body {}

Environment


[System Information]
OS Version     : Windows 10
NodeJS Version : v8.11.1
NPM Version    : 5.6.0
[Nest Information]
microservices version : 5.3.2
websockets version    : 5.3.1
mongoose version      : 5.2.1
passport version      : 5.0.1
testing version       : 5.3.1
common version        : 5.3.2
core version          : 5.3.3

Others:

@whtiehack
Copy link

@jiayisheji
Copy link
Author

@whtiehack We think differently

@kamilmysliwiec
Copy link
Member

Hi @jiayisheji,
You need to call init().

const app = await NestFactory.create(AppModule, {
    bodyParser: true,
});
await app.init();
app.use((req, res, next) => {
    console.log('req.body', req.body);
    next();
});
await app.listen(3000);

@kamilmysliwiec
Copy link
Member

By the way, bodyParser is enabled by default, you don't have to explicitly pass true.

@jiayisheji
Copy link
Author

@kamilmysliwiec Thank you

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants