我试图用NestJS / Fastify和Typescript上传文件
这是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();
这是
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;
}
将文件上传到此操作后,代码将引发如下异常
TypeError:req.pipe不是函数
在multerMiddleware(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules \ multer \ lib \ make-middleware.js:176:9)
在Promise(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ platform-express \ multer \ interceptors \ file.interceptor.js:15:81)
在新的Promise()
在MixinInterceptor.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ platform-express \ multer \ interceptors \ file.interceptor.js:15:19)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:22:36
在Object.handle(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:20:56)
在LoggingInterceptor.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ dist \ shared \ logging.interceptor.js:28:21)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:22:36
在InterceptorsConsumer.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:24:24)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ router \ router-execution-context.js:45:60
[嵌套] 10928-2020-02-06 10:10:49 [ExceptionFilter]未定义未定义+ 587529ms
TypeError:req.pipe不是函数
在multerMiddleware(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules \ multer \ lib \ make-middleware.js:176:9)
在Promise(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ platform-express \ multer \ interceptors \ file.interceptor.js:15:81)
在新的Promise()
在MixinInterceptor.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ platform-express \ multer \ interceptors \ file.interceptor.js:15:19)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:22:36
在Object.handle(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:20:56)
在LoggingInterceptor.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ dist \ shared \ logging.interceptor.js:28:21)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:22:36
在InterceptorsConsumer.intercept(D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ interceptors \ interceptors-consumer.js:24:24)
在D:\ R.Khodabakhshi \ Repository \ raimun-web \ node_modules @ nestjs \ core \ router \ router-execution-context.js:45:60
我该如何解决问题???
最佳答案
您不能将FastifyAdapter
与FileInterceptor
一起使用。 It says so in the beginning of the docs。如果要使用Fastify和文件上传,则需要为其创建自己的拦截器。