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.
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.
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,
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.
@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?
@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.
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
there is special options for this purpose in 0.7.0 now
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?.
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 ;)
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.
Most helpful comment
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 ;)