【问题标题】:Increase body limit with nestjs & fastify使用nestjs和fastify增加body限制
【发布时间】:2022-06-17 03:14:08
【问题描述】:

我的 main.ts 看起来像这样:

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

根据fastify doc,默认情况下正文限制为 1MiB,但我希望它更大。所以我尝试这样: const adapter = new FastifyAdapter({ bodyLimit: 124857600 }); 但我仍然遇到同样的问题,因为我的有效载荷太大。

【问题讨论】:

  • processRequest 函数是谁?它是否适用任何额外的限制?
  • 来自graphql-upload import { processRequest } from 'graphql-upload';

标签: nestjs fastify nestjs-fastify


【解决方案1】:

在创建应用程序时尝试添加它

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

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 2020-05-09
    • 2020-08-09
    • 2019-09-16
    • 2021-02-12
    • 2020-11-22
    • 2020-10-15
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多