mvc 模板渲染
Go to file
2023-07-21 16:13:24 +08:00
public init 2023-07-21 16:13:24 +08:00
src init 2023-07-21 16:13:24 +08:00
test init 2023-07-21 15:53:21 +08:00
views init 2023-07-21 16:13:24 +08:00
.eslintrc.js init 2023-07-21 15:53:21 +08:00
.gitignore init 2023-07-21 15:53:21 +08:00
.prettierrc init 2023-07-21 15:53:21 +08:00
nest-cli.json init 2023-07-21 15:53:21 +08:00
package.json init 2023-07-21 16:13:24 +08:00
pnpm-lock.yaml init 2023-07-21 16:13:24 +08:00
README.md init 2023-07-21 16:13:24 +08:00
tsconfig.build.json init 2023-07-21 15:53:21 +08:00
tsconfig.json init 2023-07-21 15:53:21 +08:00

模板引擎

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

async function bootstrap() {
  const app = (await NestFactory.create) < NestExpressApplication > AppModule;
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('hbs');
  await app.listen(3000);
}
bootstrap();

根目录创建这两个目录

public 公开的静态资源

views 存放模板文件

参考

文档 https://docs.nestjs.com/techniques/mvc#template-rendering

hbs https://github.com/pillarjs/hbs#readme