静态资源服务
https://docs.nestjs.com/recipes/serve-static#serve-static
https://zhuanlan.zhihu.com/p/369645693
让nestjs项目提供静态文件,主要参照以上两个文档
有很多博客使用import { NestExpressApplication } from ‘@nestjs/platform-express’,但这个没有试验成功,这些博客有可能过时了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path';
@Module({ imports: [ ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'public/uploaded'), serveRoot: '/static', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
|
这样配置之后我们可以用http://localhost:3000/static/1673278326188.jpeg
访问public/uploaded
文件夹下的图片。
图片上传
https://docs.nestjs.com/techniques/file-upload
主要参照官方文档实现
步骤一:新建Upload模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import { Module } from '@nestjs/common'; import { MulterModule } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; import { extname, join } from 'path'; import { UploadController } from './upload.controller'; import { UploadService } from './upload.service';
@Module({ imports: [ MulterModule.register({ storage: diskStorage({ destination: join('./public/uploaded'), filename: (_, file, callback) => { const fileName = `${ new Date().getTime() + extname(file.originalname) }`; return callback(null, fileName); }, }), }), ], controllers: [UploadController], providers: [UploadService], }) export class UploadModule {}
|
步骤二:新建Upload路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import { Controller, Post, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { UploadService } from './upload.service'; import { diskStorage } from 'multer'; import { extname } from 'path';
@Controller('upload') export class UploadController { constructor(private readonly uploadService: UploadService) {}
@Post('') @UseInterceptors(FileInterceptor('file')) upload(@UploadedFile() file: Express.Multer.File) { console.log('upload', file); return file; } }
|
当前写的比较简单,UploadService
没有使用,就不贴上来了。
步骤三:引入upload模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ServeStaticModule } from '@nestjs/serve-static'; import { extname, join } from 'path'; import { UploadModule } from './upload/upload.module'; import { MulterModule } from '@nestjs/platform-express'; import { diskStorage } from 'multer';
@Module({ imports: [ ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'public/uploaded'), serveRoot: '/static', }), UploadModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
|
如此依赖,我们就可以完成图片内容的上传。利用Postman测试,因为multer
只能处理multipart/form-data
,因而此处需要选择form-data
,记得勾选左侧对勾。