Git Product home page Git Product logo

express-rate-limiter's Introduction

Express Rate Limiter

What's it?

Typescript Express Rate Limiter implementation. This implementation allows you to define several things like the key to map the requests, an skip function to determine when so skip request from limiter, etc. Further on the readme you can read about the different configurations.

Installation

npm install --save @mgiamberardino/express-rate-limiter

How to use it?

ES6:

import { ExpressRateLimiter } from '@mgiamberardino/express-rate-limiter';
//const { ExpressRateLimiter } = require('@mgiamberardino/express-rate-limiter');

const app = express();
...
const limiter = new ExpressRateLimiter(options);
app.use(limiter);
limiter.start();
...
const app = app.listen( ... );
// If you use graceful shutdown you shuld stop limiter there.
process.on('exit', () => {
    limiter.stop();
});
...

Options

Using typescript you have an interface for the optiones:

import { ExpressRateLimiter, ExpressLimiterOptions } from '@mgiamberardino/express-rate-limiter';

const options:ExpressLimiterOptions = {
    limiterOptions: {
        keyMapper: ...,
        requestLimit: ...,
        timeWindow:...,
    },
    rejectStatusCode: ...,
    rejectMessage: ...,
};
const limiter = ExpressRateLimiter(options);
limiter.start();
app.use(limiter);
...

Limiter Options

Request Limit

limiterOptions: {
    ...
    requestLimit: 250, // The number of calls allowed for the same key previous to start blocking.
    ...
}

Key Mapper

limiterOptions: {
    ...
    keyMapper: (req) => req.session.user, // A function to determine an identifier for the request.
    ...
}

Time Window

limiterOptions: {
    ...
    timeWindow: 1000, // The time window in milliseconds in which the requests will be analyzed.
    ...
}

Skip Function

limiterOptions: {
    ...
    skipFunction: (req) => !req.session.user, // A function to determine if a request should be ignored by the rate limiter.
    ...
}

Reject Status Code

... = {
    ...,
    rejectStatusCode: 429, // Status code to use when rejecting requests
    ...,
}

Reject Message

... = {
    ...,
    rejectMessage: 'Too Many Requests', // Status code to use when rejecting requests
    ...,
}

express-rate-limiter's People

Contributors

mgiamberardino avatar

Watchers

 avatar  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.