Nestjs 设置静态文件,public
Posted Ajanuw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nestjs 设置静态文件,public相关的知识,希望对你有一定的参考价值。
Docs: https://docs.nestjs.com/techniques/mvc
main.js
import {
NestFactory
} from '@nestjs/core';
import {
AppModule
} from './app.module';
import { join } from 'path'
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useStaticAssets(join(__dirname, '..', 'public')) // http://localhost:5000/xxx.txt
// app.useStaticAssets('public') 跟上面一样
await app.listen(5000);
}
bootstrap();
设置虚拟路径
yarn add fastify point-of-view handlebars fastify-static
import {
NestFactory,
FastifyAdapter
} from '@nestjs/core';
import {
AppModule
} from './app.module';
import { join } from 'path'
const l = console.log;
async function bootstrap() {
const app = await NestFactory.create(AppModule, new FastifyAdapter());
app.useStaticAssets({
root: join(__dirname, '..', 'public'),
prefix: '/static/',
}); // http://localhost:5000/static/xxx.txt
await app.listen(5000)
}
bootstrap();
以上是关于Nestjs 设置静态文件,public的主要内容,如果未能解决你的问题,请参考以下文章