0
votes

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.

1
What if you try using asynchronous requests instead? Besides, do you really mean it returns a 504 after only 20s? Isn't it supposed to happen after 60s unless configured otherwise? - Stock Overflaw
@StockOverflaw - You saved my day! Using request module ( npmjs.com/package/request ) instead of sync-request worked! You can post this as an answer. - gabugu
Generally speaking with JS: if it takes too long, look for synchronous stuff. JS "seems" multi-threaded because of callbacks, promises and the like, but it's not, it's only event-based with a single process (called the event loop). Cheers! - Stock Overflaw

1 Answers

1
votes

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