Nest.js – request entity too large PayloadTooLargeError: request entity too large

Tags: , ,



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!

Answer

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();


Source: stackoverflow