0

Wondering if anyone can help? I am trying to learn how to use NestJS microservices. I have managed to succesfully get a microservice with MQTT transport setup to receive events from an API. I am trying to use pug to merge the event information with a html template file. However pug cannot find the template file...

  this.logger.info(`Running from dir ${__dirname}`); // /usr/src/app/dist/apps/microservice
  const template = pug.compileFile('../templates/email.pug');

I have used a logger to display __dirname which refers to the dist directory. This has the single file main.js inside. How do I bundle and read a template pug file from nestjs microservice?

This post has a similar question, however it is for a nestjs app.

dcs3spp
  • 493
  • 1
  • 9
  • 23

1 Answers1

0

First we need to configure the express instance

  // main.ts
  const app = await NestFactory.create<NestExpressApplication>(
       AppModule,
  );
  app.setViewEngine('hbs');

Put templates folder in root with the neighborhood src folder

enter image description here

but you must find it not in dist folder because Typescript compiler dont copy non-typescript files to dist folder for that approach you can use copy-webpack-plugin but i just advise you change this url( ../templates/email.pug) so that to find folder located in root not find it in dist hope it will help you! if not please provide your file structure. https://docs.nestjs.com/techniques/mvc

har17bar
  • 728
  • 1
  • 8
  • 22
  • Thanks for responding @har17bar. I am using NestJS microservices (https://docs.nestjs.com/microservices/basics#getting-started) and not the usual express application as created with ```NestFactory.create``` method. For the time being I am representing the pug template as a string in memory. When I get time will investigate [pug-loader](https://www.npmjs.com/package/pug-loader) in addition to your suggestion of loading the file from root. Many thanks again :) – dcs3spp Apr 02 '20 at 10:42