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

fix: body in middleware is undefined. I have bodyParser #1382

Closed
Zekhap opened this issue May 8, 2024 · 2 comments
Closed

fix: body in middleware is undefined. I have bodyParser #1382

Zekhap opened this issue May 8, 2024 · 2 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@Zekhap
Copy link

Zekhap commented May 8, 2024

Description

I'm trying to print the request.body in my console from the middleware but its undefined
I've googled everywhere and tested allot of things, yet i cant seem to find my answer.

Minimal code-snippet showcasing the problem

@Service()
@Middleware({ type: "before" })
export class DemoMiddleware implements ExpressMiddlewareInterface {
  async use(request: any, response: any, next: (err?: any) => any): Promise<any> {
    console.log("-DemoMiddleware-");

    console.log(request.body); //Is undefined
    next();
  }
}

@JsonController("/")
export class DemoController {
    @Post("test")
    demoPost(@Body() params: DemoResponse, @Res() response: Response) {
        console.log("-DemoPost-");

        return response.status(200).json({ message: "i got post", params: params });
    }
}
export class DemoResponse {
    username: string;
    password: string;
}

createExpressServer({
    controllers: [DemoController],
    middlewares: [
        bodyParser.json(),
        bodyParser.urlencoded({ extended: true }),
        DemoMiddleware
    ],
    routePrefix: '/api',
}).listen(3000);

Expected behavior

I expect to get the json that i posted

package.json
"body-parser": "^1.20.2",
"reflect-metadata": "^0.2.2",
"routing-controllers": "^0.10.4",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"typedi": "^0.10.0",

@Zekhap Zekhap added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 8, 2024
@attilaorosz
Copy link
Member

Do you use your own express instance or the lib creates it? Seems like body-parser was not running before the middleware.

@Zekhap
Copy link
Author

Zekhap commented May 13, 2024

Oh, sorry I forgot to close this
I actually tested a bit more and solved my problem.

Apparently I need to have my body parser inside a middleware.
It does not work if I use the
routing-controllers {
middlewares:{ bodyparser()}
}

@Zekhap Zekhap closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

2 participants