我试图使用hogan-express编译一个html电子邮件模板,以便在我的节点应用程序中发送出去。基本示例是一封欢迎电子邮件,它向新用户发送其登录凭据。 html模板非常简单,只需在正确的点输出数据,例如:{{username}}。我遇到的问题是我似乎无法将已编译的模板作为html提取传递给nodemailer。
基于robertlkep的真正有用的帖子我修改了我的代码,但仍然无法使事情发挥作用。我可以确认app.js文件中有以下内容:
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.set('layout', 'layouts/default');
所以该应用程序正在使用该引擎进行所有视图,并且它的工作非常好(根本不像Jade)。违规路线已更改为以下内容:
var express = require('express');
var router = express.Router();
var User = require('../models/user');
var Report = require('../models/report');
var passport = require("passport");
var localStrategy = require("passport-local").Strategy;
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
router.post('/register', function(req, res, next) {
// Registration logic etc
var password = req.body.password;
var email = req.body.email;
var company = req.body.company;
var name = req.body.name;
var role = req.body.role;
req.checkBody('email','Email field is required').notEmpty();
req.checkBody('email','Email not valid').isEmail();
req.checkBody('company','Company field is required').notEmpty();
req.checkBody('name','Name field is required').notEmpty();
var errors = req.validationErrors();
if (errors) {
res.render('register',{
errors: errors,
email: email,
company: company,
name: name,
role: role
});
} else {
var newUser = new User({
email: email,
company: company,
name: name,
password: password,
role: role
});
// Create User
User.createUser(newUser, function(err,user){
if (err) throw err;
var textContent = 'You can log in at with your email address and this password: ' + password;
app.render('emails/welcome', {
layout:"layouts/email",
title: "Email Title",
message:"Main Message",
username: email,
password:password
}, function(err,html) {
if (err) throw err;
console.log(html);
var mailOptions = {
from: 'Test Email <test@test.co.uk>',
to: email,
subject:'Subject',
text: textContent,
html: html
};
var transporter = nodemailer.createTransport(smtpTransport({
host: 'smtp.host.co.uk',
port: 465,
secure:true,
auth: {
user: 'test@test.co.uk',
pass: process.env.EMAIL_PASSWORD
}
}));
transporter.sendMail(mailOptions, function(error,info) {
if (error) {
console.log(error);
req.flash('error', "Emailed failed");
res.location('/users');
res.redirect('/users');
} else {
console.log('Message sent: ' + info.response);
// Send flash message
req.flash('success', "New user created");
res.location('/users');
res.redirect('/users');
};
});
});
});
};
});
我已经删除了任何过于具体的内容,我真的不会从test@test.com发送电子邮件。无论如何,使用此代码创建用户,但html没有登录到控制台,应用程序只是挂起。我对Node来说还是个新手,所以我犯了什么愚蠢的新手错误?
答案 0 :(得分:2)
var x = 0, y = 0;
// Execute every second
var intervalId = setInterval(function() {
x += Math.ceil(joystick.deltaX() / 20);
y += Math.ceil(joystick.deltaY() / 20);
}, 1000);
// Stop after 20 seconds
setTimeout(function() {
clearInterval(intervalId);
}, 20000)
是Express模板引擎。如果你想使用它,你必须先正确设置它:
hogan-express
如果要渲染模板并捕获生成的HTML,则可以使用app.render()
:
// assuming that `app` is your Express app instance
app.set('views', './views');
app.set('view engine', 'html');
app.engine('html', require('hogan-express'));
(或者,如果app.render('emails/welcome', { ... }, function(err, html) {
...
});
不在范围内,您也可以使用app
这假设您实际使用 Express。如果没有,请使用hogan.js
,因为在这种情况下使用res.render()
并没有多大意义。