Reputation: 1930
I'm trying to save a JSON
into a Nest.js server but the server crash when I try to do it, and this is the issue that I'm seeing on the console.log:
[Nest] 1976 - 2018-10-12 09:52:04 [ExceptionsHandler] request entity too large PayloadTooLargeError: request entity too large
One thing is the size of the JSON request is 1095922 bytes, Does any one know How in Nest.js increase the size of a valid request? Thanks!
Upvotes: 90
Views: 97365
Reputation: 461
I encountered an issue when trying to increase the body parser limit in my NestJS application. The IDE threw the following error:
Property 'useBodyParser' does not exist on type 'INestApplication<any>'.
After researching, I found the solution in the NestJS documentation. The fix involves explicitly casting the NestFactory.create
method to <NestExpressApplication>
to access the useBodyParser
method.
Here’s the code before the fix:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
And here’s the fixed version:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
// Increase the body parser limit
app.useBodyParser('json', { limit: '50mb' });
await app.listen(3000);
}
bootstrap();
Casting to NestExpressApplication
:
The useBodyParser
method is exclusive to the Express adapter. By casting NestFactory.create
to <NestExpressApplication>
, we ensure the method is available.
Custom Body Parser Configuration:
The line app.useBodyParser('json', { limit: '50mb' });
increases the JSON payload size limit to 50MB, allowing the application to handle larger request bodies.
Why Not Disable the Built-in Body Parser?
Some developers might consider disabling the built-in body parser (using { bodyParser: false }
in the NestFactory.create
options) and replacing it entirely with a custom parser or middleware. While this is possible, it’s not recommended because:
@Body()
in route handlers.app.useBodyParser()
allows you to extend the behavior of the built-in parser instead of replacing it, preserving the framework’s expected behavior.Upvotes: 2
Reputation: 75
If anyone is using @fastify/multipart here is what did the trick for me:
import multipart from '@fastify/multipart'
...
await app.register(multipart, {
limits: {
fileSize: 10 * 1024 * 1024, // 10 MB
},
})
await app.register(require('@fastify/multipart'), {
limits: {
fileSize: 5 * 1024 * 1024, // 5 MB
},
});
Upvotes: 0
Reputation: 306
In case you want to allow big size on a certain routes and restrict on others you can use:
app.use('/files', json({ limit: '10mb' }));
app.use(json({ limit: '100kb' }));
The second statement is necessary to reset the limit for all other routes
Upvotes: 0
Reputation: 37
I tried using the suggested answers of increasing the json and urlencoded limit. But the issue persisted because it was working on local but not on the server URL.
The issue was with the nginx server so I had to increase the limit too using the answer from nestjs 413 payload too large MULTIPART.
By adding to /etc/nginx/nginx.conf
client_max_body_size 5M;
So check also the max accepted by the server your application is hosted on.
Upvotes: 1
Reputation: 161
The solution that solved for me was to increase bodyLimit. Source: https://docs.nestjs.com/techniques/performance
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ bodyLimit: 10048576 }),
Upvotes: 16
Reputation: 1004
DO NOT use body-parser directly, it will break some configs by nestjs
according to https://github.com/nestjs/nest/issues/10471#issuecomment-1418091656
using app.useBodyParser
to add body-parser middleware
Upvotes: 3
Reputation: 19
I've added it like this:
import { json as expressJson, urlencoded as expressUrlEncoded } from 'express';
// You init your app here: app = await NestFactory.create(AppModule
if (app !== undefined) {
app.use(expressJson({ limit: '50mb' }));
app.use(expressUrlEncoded({ limit: '50mb', extended: true }));
}
Upvotes: 0
Reputation: 87
This resolved my issue and also in nestjs bodyparser is depreciated now so this might be an apt solution.
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
Upvotes: 4
Reputation: 1930
I found the solution, since this issue is related to express (Nest.js uses express behind scene) I found a solution in this thread Error: request entity too large,
What I did was to modify the main.ts
file add the body-parser
dependency and add some new configuration to increase the size of the JSON
request, then I use the app
instance available in the file to apply those changes.
import { NestFactory } from '@nestjs/core';
import * as bodyParser from 'body-parser';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useStaticAssets(`${__dirname}/public`);
// the next two lines did the trick
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.enableCors();
await app.listen(3001);
}
bootstrap();
Upvotes: 92
Reputation: 321
The default limit defined by body-parser is 100kb: https://github.com/expressjs/body-parser/blob/0632e2f378d53579b6b2e4402258f4406e62ac6f/lib/types/json.js#L53-L55
Hope this helps :)
For me It helped and I set 100kb to 50mb
Upvotes: 4
Reputation: 2301
you can also import urlencoded
& json
from express
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { urlencoded, json } from 'express';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));
await app.listen(process.env.PORT || 3000);
}
bootstrap();
Upvotes: 126