How to setup the edge template engine in nestjs mvc?

Viewed 21

i want to use nestjs as a MVC since i am familiar with nestjs. i want to use the edge.js template from adonisjs since it has partials and outlets and shared layouts.

i installed the edge.js lib and this is the code that i have.

import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express'
import { join } from 'path';
import { AppModule } from './app.module';

import { Edge } from 'edge.js'


(async () => {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  const edge = new Edge()
  edge.mount(join(__dirname, '..', 'views'))

  app.engine('edge', edge)

  app.setViewEngine('edge')

  await app.listen(3000);
})();

if i run the above code, i get the following error:

[Nest] 3832  - 09/21/2022, 2:10:48 PM   ERROR [ExceptionHandler] callback function required

this is the code inside my controller

 @Get()
  getHello(@Res() res: Response) {
    return res.render('welcome', { greeting: 'Hello World! from controller ----------' })
  }

what do i need to do to make the template engine work?

github to edge.js https://github.com/edge-js/edge

1 Answers

You're not registering the engine correctly. The right way is to provide a callback function:

app.engine('edge', (path, options, callback) =>
  edge
    .render(path, options)
    .catch((error) => callback(error))
    .then((rendered) => callback(null, rendered))
);

StackBlitz

Related