2
votes

I tried to upload file with NestJS/Fastify and typescript

this is main.ts

async function bootstrap() {
  //file upload with fastify
  const fastifyAdapter = new FastifyAdapter();
  fastifyAdapter.register(fmp, {
    limits: {
      fieldNameSize: 100, // Max field name size in bytes
      fieldSize: 1000000, // Max field value size in bytes
      fields: 10, // Max number of non-file fields
      fileSize: 100, // For multipart forms, the max file size
      files: 1, // Max number of file fields
      headerPairs: 2000, // Max number of header key=>value pairs
    },
  });

  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    fastifyAdapter,
  );
 await app.listen(3000);
  Logger.log('application started on http://localhost:3000', 'Bootstrap');
}
bootstrap();

and this is file.controller.ts

@Post()
  @UseInterceptors(FileInterceptor('image'))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: 'logo',
    type: UploadFileDto,
  })
  uploadedFile(@UploadedFile() file) {
    const response = {
      originalname: file.originalname,
      filename: file.filename,
    };
    return response;
  }

after uploading a file to this action, code throw an exception like this

TypeError: req.pipe is not a function at multerMiddleware (D:\R.Khodabakhshi\Repository\raimun-web\node_modules\multer\lib\make-middleware.js:176:9) at Promise (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\platform-express\multer\interceptors\file.interceptor.js:15:81) at new Promise () at MixinInterceptor.intercept (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\platform-express\multer\interceptors\file.interceptor.js:15:19) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:22:36 at Object.handle (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:20:56) at LoggingInterceptor.intercept (D:\R.Khodabakhshi\Repository\raimun-web\dist\shared\logging.interceptor.js:28:21) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:22:36 at InterceptorsConsumer.intercept (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:24:24) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\router\router-execution-context.js:45:60 [Nest] 10928 - 2020-02-06 10:10:49 [ExceptionFilter] undefined undefined +587529ms TypeError: req.pipe is not a function at multerMiddleware (D:\R.Khodabakhshi\Repository\raimun-web\node_modules\multer\lib\make-middleware.js:176:9) at Promise (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\platform-express\multer\interceptors\file.interceptor.js:15:81) at new Promise () at MixinInterceptor.intercept (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\platform-express\multer\interceptors\file.interceptor.js:15:19) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:22:36 at Object.handle (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:20:56) at LoggingInterceptor.intercept (D:\R.Khodabakhshi\Repository\raimun-web\dist\shared\logging.interceptor.js:28:21) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:22:36 at InterceptorsConsumer.intercept (D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\interceptors\interceptors-consumer.js:24:24) at D:\R.Khodabakhshi\Repository\raimun-web\node_modules@nestjs\core\router\router-execution-context.js:45:60

how can I fix the problem???

2

2 Answers

0
votes

You cannot use the FastifyAdapter with the FileInterceptor. It says so in the beginning of the docs. If you want to use Fastify and a file upload, you'll need to create your own interceptor for it.

-1
votes

Problem solved, as Jay McDaniel mentioned we couldn't use the FastifyAdapter with the FileInterceptor. I resolved the problem with this little code.

import {
  Controller,
  Logger,
  Post,
  Req,
  Res,
} from '@nestjs/common';
import * as fs from 'fs';
import * as path from 'path';
import * as pump from 'pump';
const logger = new Logger('FileController');

@ApiTags('File')
@Controller('api/file')
export class FileController {
  @Post()
  upload(@Req() req: any, @Res() reply: any): void {
      const mp = req.multipart(
      (field: any, file: any, filename: any, encoding: any, mimeType: any) => {
        console.log('save file from request ---- ', field, filename, mimeType);
        file.on('limit', () => logger.error('SIZE_LIMITED'));

        const filePath = path.resolve('./'+filename);
        const writeStream = fs.createWriteStream(filePath);
        pump(file, writeStream);
        writeStream.on('finish', () => {
          reply.code(200).send();
        });
      },

      (error: any) => {
        if (error) {
          logger.error(error);
          reply.code(500).send();
        }
      },
    );
    mp.on('partsLimit', () => logger.error('MAXIMUM_NUMBER_OF_FORM_PARTS'));
    mp.on('filesLimit', () => logger.error('MAXIMUM_NUMBER_OF_FILES'));
    mp.on('fieldsLimit', () => logger.error('MAXIMUM_NUMBER_OF_FIELD'));
  }
}

I hope this will help you too...