This repository contains an Express API with authentication and rate-limiting middleware. The rate-limiting middleware allows you to set limits for both routes protected by a unique UUID token and public routes limited by IP address. The application uses Redis to avoid statefulness, allowing for horizontal scalability. Redis's multi command is used to handle race conditions, ensuring atomic execution of commands.
- Rate limiting for protected routes based on UUID tokens.
- Rate limiting for public routes based on IP addresses.
- Redis integration for scalable and stateless architecture.
- Handling of race conditions using Redis's multi command.
- Customizable rate limits for tokens and IP addresses.
-
Clone the repository to your local machine:
git clone https://github.com/casantosmu/rate-limit-express.git cd rate-limit-express
-
Install the required dependencies:
npm install
-
Set up the environment variables by creating a
.env
file in the root directory:SERVER_PORT=3000 UUID_AUTH_TOKEN="your-uuid-auth-token" REDIS_PORT=6379 REDIS_HOST=redis RATE_LIMITER_TOKEN_LIMIT=200 RATE_LIMITER_IP_LIMIT=100
-
Run the application using npm:
npm run dev
-
Access the API through
http://localhost:3000
. -
The following routes are available:
- Public Route (Rate-limited by IP):
GET /v1/public
- Protected Route (Rate-limited by UUID token):
GET /v1/protected
- Public Route (Rate-limited by IP):
If you prefer running the application using Docker, make sure you have Docker and Docker Compose installed.
docker-compose up
Run tests using the following command:
npm test
This project is licensed under the MIT License - see the LICENSE file for details.