Git Product home page Git Product logo

connect-ratelimit's Introduction

connect-ratelimit

var limiter = require('connect-ratelimit');

app = connect()
      .use(limiter({
        whitelist: ['127.0.0.1'],
        blacklist: ['example.com']
      }))
      .use(function (req, res) {
        res.end('Hello world!');
      });

connect-ratelimit is connect middleware for limiting the number of requests per client ip/hostname to your node server.

When a limit is reached the middleware will cancel the middleware chain early with res.end('Rate limit exceeded.') or you can optionally check for a limit exceeding yourself elsewhere down the chain.

About

Categories

Categories serve as configurable templates to manage different types of connecting clients. By default all clients are categorized as 'normal' but whitelist and blacklist categories also exist.

normal

By default anyone uncategorized will be subject to 500 requests per hour.

whitelist

By default client names in the whitelist will not have their requests limited.

blacklist

By default client names in the blacklist will be subject to 0 requests per hours. In other words they will always be exceding the rate limit.

Client identification

connect-ratelimit uses the following code to identify clients:

req.headers['x-forwarded-for'] || req.connection.remoteAddress

Usage

var limiter = require('connect-ratelimit');

The middleware takes an options object with the following parameters:

  • whitelist: An array of strings representing clients you wish to apply to the whitelist category. eg. ['127.0.0.1'] for local development.
  • blacklist: An array of strings representing clients you wish to apply to the blacklist category.
  • end: A boolean when set to false (default true) the connect chain will continue even if a client has exceeded the ratelimit. The response object is augmented with the ratelimit namespace. response.ratelimit exposes an object which contains the various details about the client including if they have past their limit as well as all other recorded clients. This is useful if you wish to supply your own error response to the client or any other logic.
  • categories: An object representing the various total requests per time for each category type. See below.

Configuring the different categories

The categories property of the options object for the connect-limiter allows you to specify different totalRequests and every for specific categories.

A fully configured value of the categories property could like this:

{
  whitelist: {
    totalRequests: -1,
    every:         60 * 60 * 1000
  },
  blacklist: {
    totalRequests: 0,
    every:         60 * 60 * 1000 
  },
  normal: {
    totalRequests: 500,
    every:         60 * 60 * 1000
  }
}

Setting totalRequests to 0 is how to block requests from a category entirely.

Setting totalRequests to -1 is how to remove request limits from a category entirely.

Below is how you can switch from an hourly rate to a half-hourly rate for all categories but blacklist.

.use(limiter({
  whitelist: ['dharmafly.com'],
  categories: {
    normal: {
      every: (60 * 60 * 1000) / 2
    },
    whitelist: {
      every: (60 * 60 * 1000) / 2
    }
  }
}))

You don't need to set every category, just the properties you want to change.

Example

connect is required for the example to run.

npm install connect

To play with the example app run the command below and navigate to localhost:4000

node example.js

connect-ratelimit's People

Contributors

aaronacerboni avatar premasagar avatar jhwarnock avatar chrisnewtn avatar evanhahn avatar brotchie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.