我正在尝试部署一个简单的服务器,该服务器从我在角2前端上的联系表发送电子邮件。
我正在尝试使服务器在AWS Elastic Beanstalk上运行,但是在尝试上载并使其正常运行时,我始终收到“严重”运行状况错误。不过,它在我的本地主机环境下工作正常。
我是否缺少诸如“开始”脚本之类的内容?我需要对端口做些什么吗?这是我的第一个节点/快速服务器,所以我可能缺少一些愚蠢的东西。
这是我的server.js文件:
var express = require('express');
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.all("/*", function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
next();
});
//api key and domain for mailgun
var auth = {
auth: {
api_key: 'key',
domain: 'sandbox.mailgun.org'
}
}
//send email from angular 2 with nodemailer here
app.post('/contact-form-submit', function(req, res) {
var transporter = nodemailer.createTransport(mg(auth));
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: ' Form Submission',
text: ' Name: ' + req.body.name + ' Email: ' + req.body.email + ' Message: ' + req.body.message + ' Jobs: ' + req.body.jobs,
html: '<p>You have a new form submission: </p><br><ul><li>Name: ' + req.body.name + '</li><li>Email: ' + req.body.email + '</li><li>Jobs: ' + req.body.jobs + '</li><li>Message: ' + req.body.message + '</li></ul>'
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
res.redirect('/');
} else {
console.log('Message sent.');
res.redirect('/');
}
})
});
app.listen(3000, function() {
console.log('Express started on port 3000');
});
这是我的package.json文件:
{
"name": "project-name-here",
"version": "1.0.0",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"nodemailer": "^4.0.1",
"nodemailer-mailgun-transport": "^1.3.5"
}
}
您需要在package.json文件中添加启动脚本配置
{
"name": "project-name-here",
"version": "1.0.0",
"scripts": {
"start": "node server"
},
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"nodemailer": "^4.0.1",
"nodemailer-mailgun-transport": "^1.3.5"
}
}