From c3e81ff75d11e8a0add15b2e23e8741dc95e5ddb Mon Sep 17 00:00:00 2001 From: taolin Date: Fri, 21 Jul 2023 16:13:24 +0800 Subject: [PATCH] init --- README.md | 31 ++++++++++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 48 +++++++++++++++++++++++++++++++++++++++++-- public/1.txt | 1 + src/app.controller.ts | 13 ++++-------- src/main.ts | 7 ++++++- views/index.hbs | 12 +++++++++++ 7 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 public/1.txt create mode 100644 views/index.hbs diff --git a/README.md b/README.md index e69de29..5c00c8d 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,31 @@ +# 模板引擎 + +```js +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 diff --git a/package.json b/package.json index cf95676..7ea3daa 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", + "hbs": "^4.2.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c76acb8..c8e66fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ dependencies: '@nestjs/platform-express': specifier: ^10.0.0 version: 10.0.0(@nestjs/common@10.0.0)(@nestjs/core@10.0.0) + hbs: + specifier: ^4.2.0 + version: 4.2.0 reflect-metadata: specifier: ^0.1.13 version: 0.1.13 @@ -2583,6 +2586,10 @@ packages: resolution: {integrity: sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==} dev: true + /foreachasync@3.0.0: + resolution: {integrity: sha512-J+ler7Ta54FwwNcx6wQRDhTIbNeyDcARMkOcguEqnEdtm0jKvN3Li3PDAb2Du3ubJYEWfYL83XMROXdsXAXycw==} + dev: false + /fork-ts-checker-webpack-plugin@8.0.0(typescript@5.1.3)(webpack@5.87.0): resolution: {integrity: sha512-mX3qW3idpueT2klaQXBzrIM/pHw+T0B/V9KHEvNrqijTq9NFnMZU6oreVxDYcf33P8a5cW+67PjodNHthGnNVg==} engines: {node: '>=12.13.0', yarn: '>=1.0.0'} @@ -2770,6 +2777,19 @@ packages: resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==} dev: true + /handlebars@4.7.7: + resolution: {integrity: sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA==} + engines: {node: '>=0.4.7'} + hasBin: true + dependencies: + minimist: 1.2.8 + neo-async: 2.6.2 + source-map: 0.6.1 + wordwrap: 1.0.0 + optionalDependencies: + uglify-js: 3.17.4 + dev: false + /has-flag@3.0.0: resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==} engines: {node: '>=4'} @@ -2798,6 +2818,14 @@ packages: dependencies: function-bind: 1.1.1 + /hbs@4.2.0: + resolution: {integrity: sha512-dQwHnrfWlTk5PvG9+a45GYpg0VpX47ryKF8dULVd6DtwOE6TEcYQXQ5QM6nyOx/h7v3bvEQbdn19EDAcfUAgZg==} + engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16} + dependencies: + handlebars: 4.7.7 + walk: 2.3.15 + dev: false + /hexoid@1.0.0: resolution: {integrity: sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==} engines: {node: '>=8'} @@ -3763,7 +3791,6 @@ packages: /neo-async@2.6.2: resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==} - dev: true /node-abort-controller@3.1.1: resolution: {integrity: sha512-AGK2yQKIjRuqnc6VkX2Xj5d+QW8xZ87pa1UK6yA6ouUyuxfHuMP6umE5QK7UmTeOAymo+Zx1Fxiuw9rVx8taHQ==} @@ -4357,7 +4384,6 @@ packages: /source-map@0.6.1: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} - dev: true /source-map@0.7.4: resolution: {integrity: sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==} @@ -4768,6 +4794,14 @@ packages: hasBin: true dev: true + /uglify-js@3.17.4: + resolution: {integrity: sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==} + engines: {node: '>=0.8.0'} + hasBin: true + requiresBuild: true + dev: false + optional: true + /uid@2.0.2: resolution: {integrity: sha512-u3xV3X7uzvi5b1MncmZo3i2Aw222Zk1keqLA1YkHldREkAhAqi65wuPfe7lHx8H/Wzy+8CE7S7uS3jekIM5s8g==} engines: {node: '>=8'} @@ -4824,6 +4858,12 @@ packages: resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==} engines: {node: '>= 0.8'} + /walk@2.3.15: + resolution: {integrity: sha512-4eRTBZljBfIISK1Vnt69Gvr2w/wc3U6Vtrw7qiN5iqYJPH7LElcYh/iU4XWhdCy2dZqv1ToMyYlybDylfG/5Vg==} + dependencies: + foreachasync: 3.0.0 + dev: false + /walker@1.0.8: resolution: {integrity: sha512-ts/8E8l5b7kY0vlWLewOkDXMmPdLcVV4GmOQLyxuSswIJsweeFZtAsMF7k1Nszz+TYBQrlYRmzOnr398y1JemQ==} dependencies: @@ -4958,6 +4998,10 @@ packages: execa: 4.1.0 dev: true + /wordwrap@1.0.0: + resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==} + dev: false + /wrap-ansi@7.0.0: resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==} engines: {node: '>=10'} diff --git a/public/1.txt b/public/1.txt new file mode 100644 index 0000000..7999426 --- /dev/null +++ b/public/1.txt @@ -0,0 +1 @@ +1.txt \ No newline at end of file diff --git a/src/app.controller.ts b/src/app.controller.ts index 3ec73f5..5394bad 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, Render } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() @@ -6,13 +6,8 @@ export class AppController { constructor(private readonly appService: AppService) { } @Get() - getHello(): string { - return this.appService.getHello(); - } - @Get('/hi') - getHi() { - return { - name: 'hi', - }; + @Render('index.hbs') + getHello() { + return { message: 'Hello world!', fruit: ['apple', 'pear'] }; } } diff --git a/src/main.ts b/src/main.ts index 13cad38..7ec1048 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,13 @@ 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(AppModule); + const app = await NestFactory.create(AppModule); + app.useStaticAssets(join(__dirname, '..', 'public')); + app.setBaseViewsDir(join(__dirname, '..', 'views')); + app.setViewEngine('hbs'); await app.listen(3000); } bootstrap(); diff --git a/views/index.hbs b/views/index.hbs new file mode 100644 index 0000000..d9355c0 --- /dev/null +++ b/views/index.hbs @@ -0,0 +1,12 @@ + + + + + + Document + + +

渲染后端传来的变量: {{ message }}

+

渲染循环: {{ fruit }}

+ + \ No newline at end of file