我使用ReactJS开发了我的网络应用程序,并且我使用的是ExpressJS服务器。 Everithing工作正常,直到我实现了一个/ contactme端点,它必须作为一个宁静的POST来发送电子邮件。我的意思是,我的目标只是发布一个端点来发送邮件。
我使用快递邮件工作,但当我尝试使用发送方式发送电子邮件时,我收到此错误:
消息:"未指定默认引擎且未提供扩展名。" stack:"错误:未指定默认引擎且未提供扩展名。\ n在新视图中(c:\ React \ web.facundolarocca.com \ node_modules \ express \ lib \ view.js:62:11) \ n在EventEmitter.render(c:\ React \ web.facundolarocca.com \ node_modules \ express \ lib \ application.js:569:12)\ n在sendMail上(c:\ React \ web.facundolarocca.com \ node_modules \ express-mailer \ lib \ express-mailer.js:55:5)\ n在Object.exports.extend.createSend [as send](c:\ React \ web.facundolarocca.com \ node_modules \ express-mailer \ lib \ express-mailer.js:98:7)\ n在c:\ React \ web.facundolarocca.com \ server.js:28:16 \ n在Layer.handle [as handle_request](c:\ React \ web.facundolarocca .com \ node_modules \ express \ lib \ router \ layer.js:95:5)\ n下一步(c:\ React \ web.facundolarocca.com \ node_modules \ express \ lib \ router \ route.js:131:13 )\ n在Route.dispatch(c:\ React \ web.facundolarocca.com \ node_modules \ express \ lib \ router \ route.js:112:3)\ n在Layer.handle [作为handle_request](c:\ React) \ web.facundolarocca.com \ node_modules \表现\ LIB \ router \ layer.js:95:5)\ n在c:\ React \ web ....
我知道ExpressJS需要一些视图引擎才能返回html文件,但在我的情况下,我会回复一个简单的JSON。
以下是我的代码:
var path = require('path');
var express = require('express');
var app = express();
var mailer = require('express-mailer');
var PORT = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, '/build')));
mailer.extend(app, {
from: 'no-reply@gmail.com',
host: 'smtp.gmail.com',
secureConnection: true,
port: 465,
transportMethod: 'SMTP',
auth: {
user: '*********@gmail.com',
pass: '**********'
}
});
app.get('/', function (req, res) {
//this is working fine
res.sendFile(__dirname + '/build/index.html')
});
app.get('/contactme', function (req, res) {
try {
app.mailer.send('test', {
to: 'myemaila@myemail.com.ar',
subject: 'some subject'
}, function (err) {
if (err) {
console.log(err);
res.status(400).json('There was an error sending the email');
} else {
res.status(200).json('Email Sent');
}
});
} catch (error) {
//Error is raised here
res.status(500).json(error);
}
});
app.listen(PORT, function (error) {
if (error) {
console.error(error);
} else {
console.info("==> Listening on port %s. Visit http://localhost:%s/ in your browser.", PORT, PORT);
}
});
所以我有两个问题,一方面是如何避免这个错误,另一方面为什么我需要一个egine视图。
我只是希望从客户端读取状态代码以显示一些消息。我以为我能够以与使用restify相同的方式实现。
答案 0 :(得分:0)
我的解决方案是转移到nodemailer
。这是完整的代码:
{
"dependencies": {
"express": "^4.14.0",
"nodemailer": "^2.7.0",
"nodemailer-smtp-transport": "^2.7.2"
}
}
server.post('/contactme', (req, res) => {
try{
let mailer = require('nodemailer')
let smtpTransport = require('nodemailer-smtp-transport')
let options = {
service: config.mailService.service,
secure: true,
auth: {
user: config.mailService.user,
pass: config.mailService.password
}
}
let transport = mailer.createTransport(smtpTransport(options))
let mail = {
from: 'from',
to: 'to',
subject: 'subject',
html: 'body'
}
transport.sendMail(mail, (error, response) => {
transport.close()
if (error) {
res.json(400, {error});
} else {
res.json(200, {response: 'Mail sent.'});
}
})
} catch(error) {
res.json(400, {error});
}
})