Rate limiting prevents the same IP from making too many requests that will help us prevent attacks like brute force. The express-rate-limit is the npm package for limiting the request from the user.
Project Setup: Run the following sets of commands to create a folder and initialize the project.
mkdir test-project cd test-project npm init -y
Module Installation: Run the following command to install the express and express-rate-limit module:
npm i express express-rate-limit
Project directory: The project structure will look like this:
Run app.js file using the following command:
Output: We will see the following output on the terminal screen.
app is running on http://localhost:8000/
Output when a request doesn’t exceed the max limit of rate limiter:
Output when a request exceeds the max limit of the rate limiter:
The header of the response object has 2 fields X-RateLimit-Limit and X-RateLimit-Remaining which stores the max and remaining request from the user: