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

Can't set limit in body parser #96

Closed
snirs90 opened this issue Mar 9, 2017 · 10 comments
Closed

Can't set limit in body parser #96

snirs90 opened this issue Mar 9, 2017 · 10 comments

Comments

@snirs90
Copy link
Contributor

snirs90 commented Mar 9, 2017

I'm trying to set a "limit" with the body parser library (using express) and it is not working due to that the "routing-controller" loads the bodyParser.json() with empty options.

example:

this.app = createExpressServer({
            defaultErrorHandler: false,
            routePrefix: '/api',
            controllers: [ appPath + '/src/controllers/*.js'],
            middlewares: [ appPath + "/src/middlewares/*.js"]
        });
this.app.use(bodyParser.json({limit: '20mb'}));

This is not working because the bodyParser is called first with empty options by "JsonController" or "JsonResponse" decorators.

@snirs90
Copy link
Contributor Author

snirs90 commented Mar 9, 2017

Also I've noticed, that if I'm not using @JsonController or @JsonResponse, when I'm trying to use to populate a @Body param with a class it is not working. example:
post(@Body() candidate: CandidateForm)

If the controller class doesn't have @JsonController or the method doesn't has @JsonResponse, the candidate variable is not populated at all and remain empty.

@snirs90
Copy link
Contributor Author

snirs90 commented Mar 11, 2017

So I've manage to overcome this by configuring my own Express server and then using useExpressServer.
At the moment it is not possible to work with createExpressServer if you need to configure the body-parser json method.

@pleerock If you think that need to add the ability to configure it via createExpressServer also, I can help you out and add this feature.

Thanks,

@MichalLytek
Copy link
Contributor

It is simple - if you don't have a special requirements, you use createExpressServer but if you need some middlewares you use useExpressServer which acts as a middleware and attach it into app.use chain.

Also you can use global middlewares and configure there your 3rd party middlewares to work with routing-controllers.

@pleerock
Copy link
Contributor

pleerock commented Apr 3, 2017

@19majkel94 author is talking about these lines. Right now body-parser will be enabled anyway without configuration.

If we configure body parser on a global level, wont these lines of code override body parser of the currently executed route with empty configuration?

@MichalLytek
Copy link
Contributor

@pleerock ParamOptions (and ParamMetadataArgs) have property parseJson - shouldn't that prevent from parsing the body by body-parser?

I would like to see a global option in useExpressServer like useDefaultBodyParser: false (default true) which disable that lines and lets you configure the middleware by yourself. It's better user experience than forcing users to configure it by themselves like with @Session decorator.

Or the second option - global body-parser settings in useExpressServer. But it might have weird structure (json, text, urlencoded) so it's better to stay with user-controlled middlewares.

@pleerock
Copy link
Contributor

parseJson is not used in body.... Okay I have fixed this issue in #119 . For having separate useDefaultBodyParser please create separate issue or direct PR :D

@pleerock pleerock mentioned this issue Apr 24, 2017
@pleerock
Copy link
Contributor

there is special options for this purpose in 0.7.0 now

@cristhiamaya
Copy link

Hi @pleerock. Thank you for your work.

Sorry but, I haven't figured out yet what is exactly the solution you came up with for solving this issue. Would you be kind and please explain to me what are those "special options in 0.7.0" you mentioned before that could stop "routing-controllers" to load "bodyParser.json()" with empty options?.

@sliwalker
Copy link

sliwalker commented Dec 18, 2018

Hi @pleerock . routing-controllers is really great work. Thanks you for developing this piece of software.

I have the same issue as the topic creator. I post images as base64 and with createExpressServer() I am not able to update the limits of bodyParser. Tried to switch to useExpressServer() but then all of my requests just dont get answered anymore in case of an error (e.g. JWT expired). Using "authorizationChecker" and am returning a "UnauthorizedError" there.

So I dont know how to make useExpressServer() work for me to have the same behaviour as createExpressServer() and I dont see a chance to use createExpressServer() without the information how to configure bodyParser correctly.

Desperately need your help here. Would be awesome if you could elaborate on your latest statement from 22 May 2017, what kind of option you have integrated.

Thanks in advance.

EDIT: After some research and debugging I found out that you have added a property "options: any" in the BodyOptions of the Action (Decorator) @Body. Used it like that @Body({ options: "limit: '250mb'" }) , but that does not solve my problem of "PayloadTooLargeError: request entity too large".

EDIT2: Sorry for spaming. Guess I figured it out. Needed to pass an object to it like this:
@Body({ options: { limit: '250mb' } }).
Now it works. Thanks ;)

@github-actions
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants