Best ways to fix 504 gateway time out in nodejs Applications

This tutorial explains about below things

  • what is 504 gateway time out in nodejs

  • How to Fix 504 Gateway timeout error in NodejS or Express application

  • Express App timeout

what is 504 gateway time out in nodejs

504 Gateway Timeout is an HTTP response server-side error, that indicates an error on the server to return the response in a given timeout. The request to Server is taking longer time than expected time(default is 2 minutes)and the response contains 504 gateway timeout error.

Another cause is if the server’s down due to many reasons such as crash, out of memory, or any unhandled graceful errors.

What is 504 Gateway timeout in AWS?

504 gateway timeout error is a common error on the AWS cloud.Request to an application hosted on aws not able to return the response in a given timeout.

Applications in AWS are hosted with Webserver or load balancer enabled.

So this error is thrown when an application is not able to return a response in a given time, Elastic load balancer or web server terminates the request.

How to Fix 504 gateway timeout error in NodeJS Express APP.

NodeJS is a Javascript backend Framework. ExpressJS is a server-side framework to create and manage the server for nodejs applications. Find out

Then how do you fix this error?

Nodejs Timeout configurations

Nodejs provides Inbuilt timeout configurations in NodeJS

timeout: Related to HTTP socket timeout keepAliveTimeout: Inactive servers wait for Incoming API requests, Default is 5 seconds. headersTimeout - timeout used for parsing headers with default 60 Seconds(60000 Milli Seconds) requestTimeout - HTTP request timeout is default 2 minutes for Node 16 Version, Node 18 Version increased to 5 minutes(300000 Milli Seconds)

How to fix express JS 504 gateway timeout?

You can set the timeout in Express APp either globally or request API level.

The default timeout is 2 minutes.

To set globally, set the timeout at express object level as given below

const app = express();

// Apply server response timeout for all API requests
app.listen(port, host, function (err) {
  if (err) {
    console.error("Error in starting app", err);
  } else {"server started");
app.timeout = 240000; // default is 120000

To set a Specific API level, add a request object for API as given below."/api/test", function (req, res) {