如何使用AWS Elastic Beanstalk部署Node / Express服务器

时间:2017-08-31 18:59:32

标签: node.js amazon-web-services express elastic-beanstalk

我正在尝试部署一个简单的服务器,该服务器从我在角度2前端的联系表单发送电子邮件。

我正在尝试让服务器在AWS Elastic Beanstalk上运行,但是当我尝试上传并开始运行时,我仍然遇到“严重”的运行状况错误。但它在我的localhost环境中工作正常。

我错过了像“开始”脚本或者?我需要对我的端口做些什么吗?这是我的第一个节点/快速服务器,所以我可能错过了一些愚蠢的东西。

这是我的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: 'postmaster@.mailgun.org',
        to: 'emailname@email.com',
        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"
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在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"
  }
}