方案一 @nestjs/serve-static 库映射
上代码
npm i @nestjs/serve-static
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
const staticPath = join(__dirname, '..', '/public/');@Module({imports: [ServeStaticModule.forRoot({rootPath: staticPath,}),...],controllers: [AppController, EmailController],providers: [AppService, EmailService]
})
验证效果
访问地址你的host+路径
实例
访问地址
http://localhost:3000/images/1722302477531.png
方案二 @nestjs/platform-express 映射(nest 自带不用安装)
直接上代码
...
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
const staticPath = join(__dirname, '..', '/public');async function bootstrap() {const app = await NestFactory.create<NestExpressApplication>(AppModule, {logger: ['verbose']});app.useStaticAssets(staticPath)...await app.listen(3000);
}
bootstrap();
验证访问,同方案一