title: ExpressJS - Elastic Beanstalk 502 Bad Gateway
body: <p>I am running a Express.js app on Amazon Elastic Beanstalk and my environment was successfully created, but when I go to my environment via the URL Amazon has created, I receive a <code>502 Bad Gateway nginx/1.6.2</code> error. While I have read other resources on StackOverflow, which suggest I rename my main file from <code>server.js</code> to <code>main.js</code> and set the <code>port</code> in a <code>bin/www</code> folder, I feel like this isn't the solution for my app. I ran <code>node server.js</code>, which is the command I believe 
  Elastic Beanstalk runs and it doesn't work, (error message):</p>
  
  <pre><code>node server.js
  events.js:72
          throw er; // Unhandled 'error' event
                ^
  Error: failed to connect to [undefined:27017]
  </code></pre>
  
  <p>because I have ENV variables set up in a <code>.env</code> file, so the only way I can run my server is with foreman. This has made me think that the 502 error is the result of Elastic Beanstalk not being able to comprehend the variable and thus causing the server to fail. Can anyone confirm that I am on the right path or is this issue really because my main file is named <code>server.js</code> and not in the <code>bin/www</code> folder?</p>
  
  <p>Here is my code from my <code>server.js</code> file:</p>
  
  <pre><code>//Load express
  var express = require('express');
  var app = express();
  var router = express.Router(); // get an instance of the router
  var bodyParser = require('body-parser'); // configure app to use bodyParser()
  var mongoose = require('mongoose');
  var passport = require('passport');
  var flash = require('connect-flash');
  var morgan = require('morgan');
  var cookieParser = require('cookie-parser');
  var session = require('express-session');
  var aws = require('aws-sdk');
  
  app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
  app.use(bodyParser.json());
  app.use(morgan('dev'));
  app.use(cookieParser());
  
  
  var port = process.env.PORT || 8080; // set the port
  
  var DB_CONFIG = process.env.DB_CONFIGURATION;
  var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
  var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
  var S3_BUCKET = process.env.S3_BUCKET;
  
  var blogDB = require('./config/blogDB.js');
  mongoose.connect(blogDB.url);
  
  
  
  
  require('./config/passport.js')(passport);
  
  
  app.set('view engine', 'ejs'); // set ejs as the view engine
  
  app.use(express.static(__dirname + '/public')); // set the public directory
  
  app.use(session({ secret: 'thisisatest' }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  app.use(flash());
  
  
  var routes = require('./app/routes');
  
  app.use(routes); // use routes.js
  
  
  app.listen(port);
  console.log('magic is happening on port' + port);
  </code></pre>
  
  <p>and my package file:</p>
  
  <pre><code>{
    "name": "test",
    "main": "server.js",
    "dependencies": {
      "body-parser": "1.6.5",
      "ejs": "^1.0.0",
      "express": "^4.6.1",
      "express-paginate": "0.0.2",
      "mongoose": "~3.6.15",
      "mongoose-paginate": "^3.1.0",
      "serve-favicon": "*",
      "passport" : "~0.1.17",         
      "passport-local" : "~0.1.6",
      "connect-flash" : "~0.1.1",     
      "bcrypt-nodejs" : "latest",
      "morgan": "~1.0.0",
      "cookie-parser": "~1.0.0",
      "method-override": "~1.0.0",
      "express-session": "~1.0.0",
      "aws-sdk": "*" 
    }
  }
  </code></pre>