Valentine
Valentine

Reputation: 325

Increase body limit with nestjs & fastify

My main.ts looks like this :

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Logger } from 'nestjs-pino';
import { processRequest } from 'graphql-upload';
import { AppModule } from './app.module';

async function bootstrap() {
  const adapter = new FastifyAdapter();
  const fastifyInstance = adapter.getInstance();
  fastifyInstance.addContentTypeParser('multipart', (request, done) => {
    request.isMultipart = true;
    done();
  });

  fastifyInstance.addHook('preValidation', async (request: any, reply) => {
    if (!request.raw.isMultipart) {
      return;
    }
    request.body = await processRequest(request.raw, reply.raw);
  });
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    adapter,
    { bufferLogs: true },
  );
  app.useLogger(app.get(Logger));
  app.enableCors();
  await app.listen(parseInt(process.env.SERVER_PORT || '3000', 10), '0.0.0.0');
}

bootstrap();

According to the fastify doc the body limit is 1MiB by default, however I want it to be larger. So I tried like this : const adapter = new FastifyAdapter({ bodyLimit: 124857600 }); but I still get the same problem with my payload being too large.

Upvotes: 3

Views: 6581

Answers (1)

Avi Siboni
Avi Siboni

Reputation: 814

Try to add this when you are creating the app

const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ bodyLimit: 10048576 }),

Upvotes: 2

Related Questions