nestjsfastifynestjs-fastify

NestJS Dynamic template rendering in Fastify mode - res.render is not a function


I feel like Model-View-Controller @ docs.nestjs.com doesn't cover this use case - there is "Dynamic template rendering" section + Fastify but both combined together don't seem to work well.

I've tried the following snippet:

import { Get, Res } from '@nestjs/common'

@Get()
index(@Res() res) {
  if (isFoo()) {
    return res.render('template1.html', {var1: 'foo'})
  } else {
    return res.render('template2.html', {var2: 'bar'})
  }
}

and it fails with

TypeError: res.render is not a function

Is there any way to fix it?

Just to be clear, the reason I'm not using @Render is because I need conditional logic inside that action.


Solution

  • res.render() is the express method. For Fastify, you need to install point-of-view and use res.view()