Rate Limiting with `express-rate-limit` 🎯

beginner
9 min

Rate Limiting with express-rate-limit 🎯

Welcome to this comprehensive guide on Rate Limiting using the express-rate-limit library in Node.js! In this tutorial, we'll learn about rate limiting, its importance, and how to implement it using the express-rate-limit package in a practical, beginner-friendly manner. 📝

What is Rate Limiting? 💡

Rate limiting is a technique to control the number of requests sent to an API or a web application within a given time frame. It helps to protect the server from being overloaded and to prevent abuse or DDoS attacks.

Setting up the Project

First, let's install the required dependencies:

bash
npm init -y npm install express express-rate-limit

Now, create an app.js file and let's get started! 🚀

Implementing Rate Limiting

Creating the Express Server

javascript
const express = require('express'); const rateLimit = require('express-rate-limit'); const app = express(); // Rate limiting middleware const apiLimiter = rateLimit({ max: 100, // Limit each IP to 100 requests per day windowMs: 86400000, // 1 day in milliseconds message: 'Too many requests from this IP, please try again later.' }); app.use(apiLimiter);

In the code above, we created an Express server and applied the rate limiter to it. The apiLimiter limits each IP to 100 requests per day. If a client exceeds this limit, they will receive a friendly message explaining that they should try again later.

Creating a Protected Route

Now, let's create a protected route that users must pass through to access the API:

javascript
app.get('/api', (req, res) => { res.send('Welcome to the protected API!'); });

If you run the server and send more than 100 requests from a single IP to the /api route, you'll see the rate limiting message:

bash
curl -i -X GET http://localhost:3000/api -v ... (first request) HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 Content-Length: 25 Date: Fri, 06 May 2022 12:00:00 GMT Welcome to the protected API! ... (second request) HTTP/1.1 429 Too Many Requests Content-Type: text/plain; charset=utf-8 Content-Length: 70 Date: Fri, 06 May 2022 12:00:00 GMT Too many requests from this IP, please try again later.

Customizing Rate Limiting

You can customize the rate limiting by adjusting the max, windowMs, and message properties in the rateLimit function. For example, you can limit a user to 10 requests per minute:

javascript
const minuteLimit = rateLimit({ max: 10, // Limit each IP to 10 requests per minute windowMs: 60000, // 1 minute in milliseconds message: 'Too many requests, please slow down.' });

Quiz 📝

Quick Quiz
Question 1 of 1

What is rate limiting, and why is it important?

Wrapping Up

In this tutorial, we learned about rate limiting, its importance, and how to implement it using the express-rate-limit library. We created a simple Express server and applied rate limiting to protect a protected route. By now, you should have a good understanding of rate limiting and be able to customize it according to your needs.

Happy coding! 🎉

bash
node app.js

Stay tuned for more in-depth tutorials on Node.js and related topics at CodeYourCraft! 🚀💻🌟