使用 nestjs 将 hbs 渲染保存到变量中
Save hbs render into a variable with nestjs
我使用 hbs 在我的 nestjs 应用程序中呈现一些模板,但我需要将呈现的 html 保存到变量中并将其发送到外部 API。
在文档中 (https://docs.nestjs.com/techniques/mvc) 我只找到了如何将渲染作为正常响应发送,它有效,但我不知道如何将 html 保存为变量。
如果您想在将生成的 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)
}
);
}
}
我使用 hbs 在我的 nestjs 应用程序中呈现一些模板,但我需要将呈现的 html 保存到变量中并将其发送到外部 API。
在文档中 (https://docs.nestjs.com/techniques/mvc) 我只找到了如何将渲染作为正常响应发送,它有效,但我不知道如何将 html 保存为变量。
如果您想在将生成的 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)
}
);
}
}