Implement Request Timeout for all APIs in NodeJS server

How to implement server's request timeout for all APIs in NodeJS?

🙋‍♂️ Shubham Verma    🗓 August 8, 2022


Implement Request Timeout for all APIs in NodeJS server


Implement Request Timeout for all APIs in NodeJS server

If our API is taking more than expected time then we implement the by default request timeout at the server level. This request time will be for all APIs, if your API will take more than the expected time then your server will send the request timeout error.

In this article, we will create a server where we will implement the request timeout setup for each API. We'll implement a reusable code for request timeout So that no need to call that code in each and every API implementation.
Here, we will create a middleware and this middleware will have the logic of request timeout.

Why we need to implement request timeout?

This is a very good question "Why we need to implement request timeout?", This totally depends on your project requirements. Almost all the servers have "Request Timeout for all APIs" logic. This prevents the long waiting time for the response and also notifies the users that this operation is taking longer than expected time.
Suppose, a user request some data or want to do some operations and during this request, your server is stuck somewhere or may be some internal server error occurs then your user will wait so long until the server sends some unusual errors, the user will wait and this would not be a good user experience.

So let's implement Request Timeout for all APIs in NodeJS server

Let's have some brush-up on the following codes and concepts:

setTimeout()

setTimeout is a javascript function which is used to perform some task after a period of time. If you want to execute your code after N milisecond, then you can use setTimeout with given N milisecond.

Syntax:



app.use middlewere

Middlewares are core concepts of nodejs, every nodejs project heavily used middlewares. We use middleware to implement request timeout for all APIs in our NodeJS server.

Syntax:



Create a GET API

We will create a simple '/' GET api to call and get the simple response.

Syntax:


Let's Implement now

Create a file "app.js" and in this file, we will write our codes to implement request timeout for all APIs in NodeJS server.
See the below codes. File: app.js

Install the dependencies

Before executing this program, we need to install the dependencies by running below command:

Run the code

To run the "app.js" we need to execute the following command.

Analyze the output

After running the above codes, you need to open the browser and hit the URL 'localhost:3001/' and see the result.
The result would be based on the timeout duration that you provided in your code. If your '/' API taking more time than 'handlReqTimeout()' then the 'handlReqTimeout' will be executed and you will get a 'Timeout' message and if the '/' API taking less time than handlReqTimeout() which is 2000 milliseconds then you will get 'Hello World!'.

Conclusion:

In this article, we learned how to implement the Request Timeout for all APIs in NodeJS server along with some other JS concepts.

Related Keywords:

Nodejs HTTPS Request Timeout

Express timeout middleware

Node server timeout express

How to increase connection timeout in node js

Nodejs request timeout error

Request Timeout for all APIs




Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.