【发布时间】:2017-01-20 19:53:35
【问题描述】:
我已经使用 ReactJS 开发了我的网络应用程序,并且我正在使用 ExpressJS 作为服务器。在我实现了一个 /contactme 端点之前,一切都运行良好,该端点必须作为 restful POST 发送电子邮件。我的意思是,我的目标只是发布一个端点来发送邮件。
我正在使用 express-mailer 来完成这项工作,但是当我尝试使用 send 方法发送电子邮件时,我收到了这个错误:
消息:“没有指定默认引擎,也没有提供扩展。” 堆栈:“错误:未指定默认引擎并且未提供扩展名。\n at new View (c:\React\web.facundolarocca.com\node_modules\express\lib\view.js:62:11)\n at 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 [作为发送] (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 [as handle_request] (c:\React\web. facundolarocca.com\node_modules\express\lib\rou ter\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 view。
我只是希望从客户端读取状态代码以显示一些消息。我认为我可以像使用 restify 一样实现。
【问题讨论】:
-
需要视图引擎来呈现您要发送的电子邮件 html。为避免错误,请阅读文档 npmjs.com/package/express-mailer#views 或不要使用 express-mailer 并使用其他不需要视图引擎的东西。
-
可能我需要改用 nodemailer 之类的东西。经过太多的阅读,我怀疑你回答了我什么。