【问题标题】:Save hbs render into a variable with nestjs使用nestjs将hbs渲染保存到变量中
【发布时间】:2019-09-03 11:49:05
【问题描述】:

我使用 hbs 在我的 nestjs 应用程序中渲染一些模板,但我需要将渲染后的 html 保存到变量中并将其发送到外部 API。

在文档 (https://docs.nestjs.com/techniques/mvc) 中,我只找到了如何将渲染作为正常响应发送,它可以工作,但我不知道如何将 html 保存为变量。

【问题讨论】:

标签: javascript typescript handlebars.js nestjs


【解决方案1】:

如果您想在将生成的 HTML 发送到客户端之前访问它,您可以使用响应 render 方法的第三个参数:

app.controller.ts

import { Get, Controller, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller()
export class AppController {
  @Get()
  root(@Res() res: Response) {
    return res.render(
        'index',
        { message: 'Hello world!' },
        function (err, html) {
            // Here you have access to the generated HTML
            res.send(html)
        }
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多