nestJS前端项目包部署过程

  • Post author:
  • Post category:其他




新建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'),
    );
  }
}



静态资源放置

在这里插入图片描述



项目运行

  • 浏览器输入:

    http://localhost:3000/react-ant-admin/


    在这里插入图片描述



版权声明:本文为weixin_38007986原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。