Mujib517 / request-throttler-express

A simple request rate throttler for node and express applications.

Home Page:https://www.npmjs.com/package/request-throttler-express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request Throttler Express

A simple request rate throttler for node and express applications.

Installation

The following command will install and set it up for your current project:

    npm install request-throttler-express

Currently you need to have a redis server running for using this module. More caching stores will be added soon.

Usage

Request throttler can be used just as any other standard express.js middleware.

    const requestThrottler = require('request-throttler-express');
    
    // Register as a middleware
    app.use(requestThrottler());

By default, it will limit to 10 requests per minute. To take more control, check out the possible options you can pass to the middleware below.

Options

The middleware function accepts a variety of optional parameters.

  • minutesWindow

    The size of the time window in minutes. Default value is 1.

  • maxHits

    The maximum number of requests that can be made from any particular IP in a given window. Default value is 10.

  • ipGetter

    This parameter accepts a function with the request object as the argument. It expects the user to return the ip address as a string. The default implementation retrieves the standard express req.ip.

  • limitExceededHandler

    This parameter accepts a function with the request object as the argument. The user can do logging and similar things based on their requirements here. By default it's a no-op.

Example Usage

The options can be used in any combination like following:

    app.use(requestThrottler({
        minutesWindow: 5,
        maxHits: 20,
    }));

About

A simple request rate throttler for node and express applications.

https://www.npmjs.com/package/request-throttler-express

License:MIT License


Languages

Language:JavaScript 100.0%