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();

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/9574535.html