Nest.js - request entity too large PayloadTooLargeError: request entity too large
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();
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();
The solution that solved for me was to increase bodyLimit. Source:https://www.fastify.io/docs/latest/Server/#bodylimit
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ bodyLimit: 10048576 }),