1
votes

I'm serving an application via AWS Elastic Beanstalk. localhost and ngrok serve the webpage and additional api calls as expected. When uploaded to AWS-ELB a 504 error is the response for any additional api calls after the webpage is served.

The application serves the base index.html file via:

BASIC SETUP AND SERVING INDEX.HTML

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.all('/', ensureSecure)
function ensureSecure(req, res, next){
  if(req.headers['x-forwarded-proto'] === 'https'){
    // OK, continue
    return next()
  };

   res.redirect('https://' + req.headers.host)
}
app.use(express.static(__dirname + '/../web'), () => {}) // SERVE THE WEBPAGE

var port = process.env.PORT || 443

var router = express.Router()

router.use(function(req, res, next) {

    console.log('here')
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    next(); // make sure we go to the next routes and don't stop here
});

It then has additional routes below it:

SUBSEQUENT API CALL

app.get('/user/:type', function(req, res) {
  res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

  var dbparams = {};
  dbparams.TableName = 'FFUserTable';
  dbparams.KeyConditionExpression = 'uid = :uid'
  dbparams.ExpressionAttributeValues = {':uid':req.query.uid};
  ddb_calls.awsDynamoQuery(dbparams, function(data){
    res.json(data);
  })

})

The index page serves and renders just fine. But any of the routes below it return a 504 gateway error when the page makes calls to them. The strange thing is that this only happens when deployed to AWS-ELB. Everything works as expected when using localhost or when using ngrok. Even if I take out the https redirect I get the same issue.

AWS-ELB ssl is setup and routed correctly via AWS-Route53.

1
504 means "gateway timeout", so the request that is getting passed to your Node app is timing out. Can you add the code for one of the routes that's timing out to your question?robertklep
Updated. The route works fine in every other situation except as described above. It even works on ELB if I separate the front-end server and back-end onto separate servers. It only has begun to fail now that I'm serving index.html from the same server.Phil Andrews
It seems to me that ddb_calls.awsDynamoQuery might be causing the timeout.robertklep
No that's not it. There are about dozen other routes and it happens in any of the additional routes regardless of the code inside the route.Phil Andrews
Oh hang on, I see the issue now...robertklep

1 Answers

1
votes

This is the problem:

app.use(express.static(__dirname + '/../web'), () => {})

I'm not sure why you added that last function there, but it's an (incomplete) middleware function that will stall all requests because it's not doing anything.

It should be this:

app.use(express.static(__dirname + '/../web'))