express-rate-limiter-middleware

1.0.2 • Public • Published

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:

  1. Import the required modules:
const express = require('express');
const redis = require('redis');
const { RateLimit } = require('express-rate-limit');
const { RedisStore } = require('rate-limit-redis');
  1. Create an Express app and specify the port:
const app = express();
const port = process.env.PORT || 3000;
  1. Create a Redis client:
const redisClient = redis.createClient();
  1. 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);
  1. Define your routes as usual:
app.get('/', (req, res) => {
  res.send('Hello World!');
});
  1. 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.

Package Sidebar

Install

npm i express-rate-limiter-middleware

Weekly Downloads

1

Version

1.0.2

License

ISC

Unpacked Size

4.78 kB

Total Files

5

Last publish

Collaborators

  • deionl