66

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!

Alexisvt
  • 1,561
  • 1
  • 10
  • 9

8 Answers8

94

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();
shahidfoy
  • 1,951
  • 1
  • 17
  • 23
  • 3
    much better solution than adding extra library – Nenad Jovicic May 18 '21 at 11:15
  • 1
    @NenadJovicic actually the `body-parser` is already included and used by `express` and author of NestJS recommends using it: https://github.com/nestjs/nest/issues/529#issuecomment-376576929 So it's most probably the same. – icl7126 Oct 29 '21 at 08:29
79

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();
Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
Alexisvt
  • 1,561
  • 1
  • 10
  • 9
  • if express verion is higer than 4.x. the code is like ```app.use(express.urlencoded({ limit: '50mb' }))``` https://expressjs.com/en/4x/api.html#express-json-middleware – Sacru2red Jan 17 '23 at 07:43
14

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 }),
Brenner Batista
  • 141
  • 1
  • 2
3

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

saddam
  • 301
  • 3
  • 11
1

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'}));
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Raja
  • 57
  • 9
0

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 }));
}
Prom0
  • 19
  • 3
0

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

Diluka W
  • 844
  • 8
  • 10
0

Its helps for me:

app.use(json({ limit: '50mb' }));
Tyler2P
  • 2,324
  • 26
  • 22
  • 31