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 502 Bad Gateway nginx/1.6.2
error. While I have read other resources on StackOverflow, which suggest I rename my main file from server.js
to main.js
and set the port
in a bin/www
folder, I feel like this isn't the solution for my app. I ran node server.js
, which is the command I believe
Elastic Beanstalk runs and it doesn't work, (error message):
node server.js
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [undefined:27017]
because I have ENV variables set up in a .env
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 server.js
and not in the bin/www
folder?
Here is my code from my server.js
file:
//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);
and my package file:
{
"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": "*"
}
}
Error: failed to connect to [undefined:27017]
. Did you try setting upNODE_ENV
to right value from EBS console? – Dupondius