node.js - 如何在 nestjs 框架中使用 nunjucks 模板?

标签 node.js nestjs

如何正确使用 NestExpressApplication 下的 nunjucks 模板?

最佳答案

对于那些想要使用 Nunjucks 的人模板引擎 NestJS框架并发现了这个问题,这里是一个最小的例子:

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

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const express = app.getHttpAdapter().getInstance();

  const assets = path.join(__dirname, '..', 'assets'); // Directory with static HTML/CSS/JS/other files
  const views = path.join(__dirname, '..', 'views'); // Directory with *.njk templates

  nunjucks.configure(views, { express });
  
  app.useStaticAssets(assets);
  app.setBaseViewsDir(views);
  app.setViewEngine('njk');

  await app.listen(3000);
}
bootstrap();

关于node.js - 如何在 nestjs 框架中使用 nunjucks 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56057228/

相关文章:

postgresql - TypeORM 使用 ST_GeomFromGeoJSON 插入几何类型

postgresql - 如何在 Prisma ORM 中过滤关系

javascript - Nestjs Swagger UnhandledPromiseRejectionWarning : TypeError: Cannot destructure property `prototype` of 'undefined' or 'null'

javascript - 更新 MongoDB 文档列表

node.js - TypeError : Joi. 验证不是一个函数

javascript - 任意 JS/HTML 上传的 Web 安全

http - 使用 ExpressJS 提供非标准 HTTP 方法

javascript - NestJS/Swagger : what model is the ApiExtraModel expecting as a parameter?

node.js - Passport -facebook FacebookTokenError : This authorization code has been used

nestjs - HTTP 和 RPC 之间的共享异常