A
A
AlexMark2017-07-16 01:01:19
Node.js
AlexMark, 2017-07-16 01:01:19

How to get the html code of a page rendered through handlebars in node.js?

I'm following the node.js tutorial. The tutorials use pug, but I use hbs. I translated more or less successfully until I ran into a problem with rendering letters. In the lesson itself, this code

const nodemailer = require('nodemailer');
const pug = require('pug');
const juice = require('juice');
const htmlToText = require('html-to-text');
const promisify = require('es6-promisify');

const transport = nodemailer.createTransport({
 host: process.env.MAIL_HOST,
 port: process.env.MAIL_PORT,
 auth: {
  user: process.env.MAIL_USER,
  pass: process.env.MAIL_PASS
 }
});

 const generateHTML = (filename, options = {}) => {
  const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`, 
  options);
   const inlined = juice(html);
return inlined;
 };

 exports.send = async (options) => {
   const html = generateHTML(options.filename, options);
   const text = htmlToText.fromString(html);

  const mailOptions = {
    from: `Wes Bos <[email protected]>`,
    to: options.user.email,
    subject: options.subject,
    html,
    text
  };
   const sendMail = promisify(transport.sendMail, transport);
   return sendMail(mailOptions);
  };

Well, in fact, when the user clicks on the button, a letter is generated from the template with data from (options). I don’t understand how I can also repeat renderFile, only for hbs file types ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abcdefgk, 2017-07-16
@Abcdefgk

Is the problem in this line - const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`)?
The handlebars seems to have a method called compile
const html = hbs.compile(`${__dirname}/../views/email/${filename}.hbs`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question