nestjs 413有效载荷太大MULTIPART

时间:2020-02-21 02:54:31

标签: javascript node.js express nestjs multer

我使用的是nestjs,并且端点通过分段仅接受文件,当我尝试上传<10MB的文件时,它们都可以正常工作,但是当我尝试> 10Mb的文件时,我总会得到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

@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> {

0 个答案:

没有答案