Post/Code

HomeAboutUsesNow

Node.js, Nodemailer + Express Handlebars, Express + TypeScript.

  • Nodemailer-express-handlebars is a useful plugin for Nodemailer that allows you to use Handlebars templates in your HTML emails | Link
    The documentation on that page does not provide a full usage example and is really short on details. I then found this article which was incredibly helpful! | Link

    ...
    var hbs = require('nodemailer-express-handlebars');
    var options = {
    viewEngine : {
        extname: '.hbs', // handlebars extension
        layoutsDir: 'views/email/', // location of handlebars templates
        defaultLayout: 'template', // name of main template
        partialsDir: 'views/email/', // location of your subtemplates aka. header, footer etc
    },
    viewPath: 'views/email',
    extName: '.hbs'
    };
    ...
    mailer.use('compile', hbs(options));
    mailer.sendMail({
    from: 'test@test.com',
    to: req.body.to,
    subject: req.body.subject,
    template: 'template',
    context: {
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        ...
    }
    }, function(error, response)...) 
    
  • Most of my day was then spent trying to figure out how best to put Node.js, Express and its dependencies into a working app via Webpack. I have not been very successful so far... My main issue at present is that when I run Webpack I get a lot of errors that say things like: Module not found: Error: Can't resolve './decode/' etc
    I did however, find two articles which look promising and that I will be picking for clues:

  • TypeScript, Express and Node.js | Link
    This is a great article, however, he uses Grunt to build his TypeScript which is not quite what I am looking for.
  • TypeScript, ExpressJS API with Webpack | Link
    Another great article that look to cover most of what I am looking for.