nestJS部署前端项目包

788 阅读1分钟

新建nestJS项目

  • main.ts文件
    • 静态资源目录放置在public文件下
import { NestExpressApplication } from '@nestjs/platform-express';
import * as path from 'path';

const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: true,
  });
  app.useStaticAssets(path.join(__dirname, '..', 'public'));
  app.setBaseViewsDir(path.join(__dirname, '..', 'views'));
  app.setViewEngine('hbs');
  // 设置统一的接口前缀
  app.setGlobalPrefix('/react-ant-admin');
  • app.controller.ts文件
import { Response } from 'express';
import * as path from 'path';

  getHello(@Res() res: Response): any {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    res.sendFile(
      path.join(__dirname, '../public/react-ant-admin', 'index.html'),
    );
  }
}

静态资源放置

Xnip2022-09-17_16-32-02.jpg

项目运行

  • 浏览器输入:http://localhost:3000/react-ant-admin/

Xnip2022-09-17_16-35-37.jpg