Navigate back to the homepage

integrate Mustache nodejs example

Infinitbility
Node, Mustache
May 1st, 2021 · 1 min read
integrate Mustache nodejs example

Hello Friends,

Welcome To Infinitbility!

This article explain and provide example to implement Mustache template in node js. we are assuming you want send mustache template content in your mail function.

Let’s start today topic integrate mustache template in nodejs with typescript

  1. Start with Installation

    1npm install consolidate mustache --save

    install consolidate and mustache library to setup mustache render engine

  2. Setup mustache engine

    app.ts

    • import consolidate engine
    1import engine from "consolidate";
    • create private function initializeMailTemplate
    1private initializeMailTemplate(){
    2
    3 // set templates folder
    4 this.app.set('views', __dirname + '/views');
    5
    6 // // set html templating engine
    7 this.app.engine('html', engine.mustache);
    8 this.app.set('view engine', 'html');
    9}
    • call from constructor
    1constructor(routes: Routes[]) {
    2 this.app = express();
    3 this.initializeMailTemplate();
    4}
  3. use engine in nodejs controler for render template

    mails.controller.ts

    1import { NextFunction, Request, Response } from 'express';
    2import sendMail from "@utils/sendMail";
    3class MailsController {
    4
    5 /**
    6 * Send Mail
    7 *
    8 * @param req
    9 * @param res
    10 * @param next
    11 */
    12 public emails = async (req: Request, res: Response, next: NextFunction) => {
    13 try {
    14
    15 const email: string = req.body.email;
    16 const template: string = req.body.template;
    17 const template_data: object = req.body.template_data;
    18 // template will be your template file name
    19 res.render(template, template_data, function (err, html) {
    20 const msg = {
    21 to: email, // Change to your recipient
    22 from: process.env.SENT_MAIL_ID, // Change to your verified sender
    23 subject: 'Welcome To Infinitbility',
    24 html: html,
    25 }
    26
    27 sendMail(msg);
    28 });
    29
    30 res.status(200).json({ message: 'Mail Sent Successfully' });
    31 } catch (error) {
    32 next(error);
    33 }
    34 };
    35}
    36
    37export default MailsController;
  4. Create html template

    • create views folder on your src folder and add below registration template

    registration.html

    1<!DOCTYPE html>
    2 <html>
    3 <body>
    4 <p>
    5 Hi {{name}},
    6
    7 Thank You For Registration.
    8 </p>
    9 </body>
    10 </html>

Thanks For Reading…

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

Safemode in windos 7 and 10

Safemode in windos 7 and 10

how to go Safemode in windos 7 and 10

April 30th, 2021 · 1 min read
How to install drivers on your windows

How to install drivers on your windows

install drivers on your windows os

April 29th, 2021 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]