express-rate-limiter-middleware
v1.0.2
Published
An Express middleware for rate limiting requests using Redis.
Downloads
2
Maintainers
Readme
express-rate-limiter-middleware
express-rate-limiter-middleware
is a middleware for Express.js applications that provides rate limiting functionality to control the number of requests a client can make to the server within a specified time frame.
Installation
You can install express-rate-limiter-middleware
via npm:
npm install express-rate-limiter-middleware
Usage
To use express-rate-limiter-middleware
in your Express.js application, follow these steps:
- Import the required modules:
const express = require('express');
const redis = require('redis');
const { RateLimit } = require('express-rate-limit');
const { RedisStore } = require('rate-limit-redis');
- Create an Express app and specify the port:
const app = express();
const port = process.env.PORT || 3000;
- Create a Redis client:
const redisClient = redis.createClient();
- Set up the rate limiter middleware:
const limiter = new RateLimit({
store: new RedisStore({
client: redisClient
}),
windowMs: 1 * 60 * 1000, // 1 minute
max: 100, // limit each IP to 100 requests per windowMs
message: "Too many requests, please try again later."
});
// Apply rate limiter to all routes
app.use(limiter);
- Define your routes as usual:
app.get('/', (req, res) => {
res.send('Hello World!');
});
- Start the server:
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Documentation
For more advanced usage and customization options, please refer to the documentation.
License
This project is licensed under the MIT License - see the LICENSE file for details.