我正在尝试设置一个快速应用程序,以使用SendGrid和NodeMailer发送电子邮件。我已经获得了我的vanilla节点应用程序来发送电子邮件,但我还没有弄清楚如何路由应用程序。有关如何路由应用程序的任何想法?这就是我到目前为止所做的:
var express = require('express');
var router = express.Router();
var app = express();
app.post('/postEmail', function (req, res) {
var nodemailer = require('nodemailer');
var sendgridTransport = require('nodemailer-sendgrid-transport');
//insert api keys
var credentials = {
auth: {
api_key:'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
};
//create new mailer using the SendGrid transport
var createMail = nodemailer.createTransport(sendgridTransport(credentials));
var emailBody = {
to: ['xxx@xx.com'],
from: 'xxx@xx.com',
subject: 'xx',
text: 'xxx'
};
//send the email
createMail.sendMail(emailBody, function (err, res) {
if (err) {
console.log(error)
}
console.log(res);
});
});
// finalize the routers to be used and register them
app.use('/', router);
//start the server on the specified port
app.listen(2021);
console.log('You can access the API on port ' + 2021+".");