Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save hbs render into a variable with nestjs

I use hbs to render some templates in my nestjs app, but I need to save the html renderized into variable and send it to a external API.

In docs (https://docs.nestjs.com/techniques/mvc) I only found how to send the render as a normal response, It works, but I don't know how to save the html as variable.

like image 370
Dicren Avatar asked Apr 10 '26 15:04

Dicren


1 Answers

If you want to have access to the generated HTML before sending it to the client you can use the third parameter of the response render method:

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)
        }
    );
  }
}
like image 73
mgarcia Avatar answered Apr 12 '26 05:04

mgarcia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!