bound 默认设置根目录下的 public 文件夹为静态文件存放位置。
// main.ts
import { NestFactory } from '@nestjs/core'
import { Logger } from '@nestjs/common'
import { NestExpressApplication } from '@nestjs/platform-express'
import { AppModule } from './app.module'
import config from './config'
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule)
...
...
app.useStaticAssets(`${config.APP}/public`); // 设置静态文件位置
await app.listen(config.PORT)
Logger.log(`app is listening to ${config.PORT}`)
}
bootstrap();
以下面的文件结构为例
访问 http://localhost:8080/logo.png 可以看到如下效果:
更多静态服务相关的内容,可以参考 静态服务。