Jeong Ki Hun
Jeong Ki Hun

Reputation: 135

Nodejs 502 Bad Gateway Deploying Express on Elastic Beanstalk AWS

I deployed node js application to AWS EBS. When I run the app, i get the error " 502 Bad Gateway " nginx/1.6.2 . This is what i found out in the log.

2016/08/13 08:46:03 [warn] 14418#0: duplicate MIME type "text/html" in /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf:42
2016/08/13 09:22:25 [error] 14421#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 118.36.218.138, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com"
2016/08/13 09:22:25 [error] 14421#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 118.36.218.138, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com", referrer: "http://cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com/"

I spend my day for this. and i found some good solution. enter link description here (it's same problem) but it's not working on my code.( I tried the rename solution which most chosen ) While I have read other resources on StackOverflow, which suggest I rename my main file from app.js to main.js and set the port in a bin/www folder, I feel like this isn't the solution for my app.

{
  "name": "cidermics",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node main.js"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "ejs": "~2.3.3",
    "express": "~4.13.1",
    "file-system": "^2.2.1",
    "formidable": "^1.0.17",
    "morgan": "~1.6.1",
    "multer": "^1.1.0",
    "node-dir": "^0.1.11",
    "serve-favicon": "~2.3.0",
    "xhr": "^2.2.0",
    "passport" : "*",
    "passport-local" : "*",
    "connect-flash" : "*",
    "express-session" : "*",
    "req-flash" : "*"
  }
}

It's main.js I changed the name from app.js to main.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var debug = require('debug')('cidermics:server');
var http = require('http');
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
var mysql = require("./routes/model/mysql");
var flash = require('req-flash');
var session = require('express-session');

var routes = require('./routes/index');
var users = require('./routes/users');
var admin = require('./routes/admin');

//route add
var about = require('./routes/about');
var cmn = require('./routes/cmn');
var consulting = require('./routes/consulting');
var contents = require('./routes/contents');
var member = require('./routes/member');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'fortt', resave: true, saveUninitialized: true }))

app.use(passport.initialize());
app.use(passport.session());

//express.static ADD
app.use('/cid_about', express.static(__dirname + '/views/cid_about'));
app.use('/cid_cmn', express.static(__dirname + '/views/cid_cmn'));
app.use('/cid_consulting', express.static(__dirname + '/views/cid_consulting'));
app.use('/cid_contents', express.static(__dirname + '/views/cid_contents'));
app.use('/cid_member', express.static(__dirname + '/views/cid_member'));
app.use(flash());




app.use('/', routes);
app.use('/users', users);
app.use('/adm', admin);

//app.get
app.use('/',about);
app.use('/',cmn);
app.use('/',consulting);
app.use('/',contents);
app.use('/',member);


passport.use('local', new LocalStrategy({
	
    usernameField : 'email',
    passwordField : 'pw',
    passReqToCallback : true
}

,function(req, email, pw, done) {
	
	mysql.select('select * from cider.cid_user where user_email ="'+email+'" and user_password = "'+pw+'"', function (err, data){
		console.log("data");
		console.log(data.length);
		if(data.length < 1){
			console.log('fail');
			return done(null, false);
		}else {
			console.log('success');
			return done(null, data);
		}
		if(err){
			res.redirect('back');
		}
		
    });
	
}
));

passport.serializeUser(function(user, done) {
    done(null, user);
    // if you use Model.id as your idAttribute maybe you'd want
    // done(null, user.id);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

app.set('port', process.env.PORT || 80);

var server = app.listen(app.get('port'), function() {
//http.createServer(app).listen(app.get('port'), function(){
//	console.log('Express server listening on port ' + app.get('port'));

  debug('Express server listening on port ' + server.address().port);
});

/var/log/nodejs/nodejs.log
-------------------------------------
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm  v2.15.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node app.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node app.js'.
npm ERR! This is most likely a problem with the cidermics package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs cidermics
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls cidermics
npm ERR! There is likely additional logging output above.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm  v2.15.5
npm ERR! path npm-debug.log.3664877195
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open

npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.3664877195'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, open 'npm-debug.log.3664877195']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: 'npm-debug.log.3664877195' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /var/app/current/npm-debug.log

> [email protected] start /var/app/current
> node app.js

/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:90
                    throw err0;
                    ^

Error: EACCES: permission denied, mkdir '/var/app/public'
    at Error (native)
    at Object.fs.mkdirSync (fs.js:794:18)
    at sync (/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:71:13)
    at Function.sync (/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:77:24)
    at new DiskStorage (/var/app/current/node_modules/multer/storage/disk.js:21:12)
    at module.exports (/var/app/current/node_modules/multer/storage/disk.js:65:10)
    at new Multer (/var/app/current/node_modules/multer/index.js:15:20)
    at multer (/var/app/current/node_modules/multer/index.js:88:12)
    at Object.<anonymous> (/var/app/current/routes/admin.js:8:14)
    at Module._compile (module.js:409:26)

npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm  v2.15.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node app.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node app.js'.
npm ERR! This is most likely a problem with the cidermics package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs cidermics
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! 
npm ERR!     npm owner ls cidermics
npm ERR! There is likely additional logging output above.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm  v2.15.5
npm ERR! path npm-debug.log.1654992227
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open

npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.1654992227'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, open 'npm-debug.log.1654992227']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: 'npm-debug.log.1654992227' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /var/app/current/npm-debug.log

Upvotes: 3

Views: 2167

Answers (1)

Sam H.
Sam H.

Reputation: 4349

Your dependency multer is running the command mkdir. But since it is the node process invoking this, and the node process doesn't have permission to run shell commands, it is throwing an error.

How important is multer? Your code snippets don't show you using it. Maybe you can remove it?

Here is a related question.

Upvotes: 1

Related Questions