Search code examples
javascriptnode.jsexpresstimer

504 Gateway Timeout error on Express.js when a timer is running


I am getting "504 Gateway Timeout" error on my Express.js app when a JavaScript timer is running every 20 seconds.

This is my Express.js listener and the timer:

const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('ok.'))
var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

var items = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]

let forParallel = require('node-in-parallel');
var request = require('sync-request');

setInterval(function() {
  forParallel(items, function(listItem) {
        request("GET", "https://example.com/?i=" + listItem);
        console.log("Done.");
  }, function () {
  });
}, 20000);

So, as you can see: I am using node-in-parallel to loop on the array items every 20 seconds parallely... each loop iteration should do a GET request to "https://example.com/?i=" + listItem ...

So.. Express.js works fine before the timer starts for the first time! but when the timer starts Express.js returns "504 Gateway Timeout Error"

Isn't the Express.js listener async?

EDIT: Note that the process of the GET request takes time (like 30 seconds per iteration) because it does something big.


Solution

  • As per the comments, problem was synchronous requests preventing route listeners from answering in due time.