гнездо js 413 слишком большая полезная нагрузка MULTIPART - PullRequest
0 голосов
/ 21 февраля 2020

Я использую гнездо js, и конечная точка принимает только файлы через multipart, когда я пытаюсь загрузить файлы размером <10 МБ, все работает отлично, но когда я пытаюсь использовать файлы размером> 10 МБ, я всегда получаю 413. Я читаю много постов с этим

 app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true,
    parameterLimit: 50000
  }));

Но у меня не работает, я использую multer, поэтому я тоже оставлю конфигурацию.

Main.ts

async function bootstrap() {
  Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  app.use(helmet());
  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true,
    parameterLimit: 50000
  }));
  app.enableCors();
  app.use(
    rateLimit({
      windowMs: 15 * 60 * 1000, // 15 minutes
      max: 20000, // limit each IP to 20000 requests per windowMs
    }),
  );
  app.use(compression());
  const options = new DocumentBuilder()
    .setTitle('Latineo Api')
    .setDescription('Api documentation for latineo')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}

MyModule с конфигурацией multer

@Module({
  controllers: [RestaurantsController],
  imports: [
    RestaurantsModule,
    MongooseModule.forFeature([{ name: 'Restaurant', schema: RestaurantSchema }]),
    GlobalModule,
    UsersModule,
    ConfigModule,
    MulterModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        storage: diskStorage({
          destination: `./${configService.uploadImages}`,
          filename: (req, file, cb) => {
            cb(null, `${uuid()}${extname(file.originalname)}`);
          },
        }),
        fileFilter: (req, file, cb) => {
          if (file.mimetype.match(/\/(jpg|jpeg|png)$/)) {
            cb(null, true);
          } else {
            cb(
              new BadRequestException(
                'The image format is not valid only jpg, png are a valid format',
              ),
              false,
            );
          }
        },
        limits: {
          fileSize: 104857600,
          files: 10
        },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [UtilsService, RestaurantsService],
  exports: [RestaurantsService],
})

информация запроса enter image description here

заголовок конечной точки

@ApiBearerAuth()
  @UseGuards(FirebaseAuthGuard)
  @UseInterceptors(FilesInterceptor('imageUrls'))
  @ApiConsumes('multipart/form-data')
  @ApiFiles('imageUrls')
  @Put(':id/images')
  async uploadImagesRestaurant(
    @Param('id') id: string,
    @UploadedFiles() imageUrls,
    @Req() request: any,
  ): Promise<RestaurantI> {
...