GithubHelp home page GithubHelp logo

yet-another-rate-limiter's Introduction

Yet Another Rate Limiter

Yet another rate limiter for express. This defaults to using an in-memory store which should only be used for development purposes. A redis backed store is included in the package, and should be used for production.

Usage

For an API-only server where the rate-limiter should be applied to all requests:

const express = require("express");
const rateLimiter = require("yet-another-rate-limiter")({
  maxRequests = 100, // max number of requests a user can make in a particular timeFrame
  timeFrame = 3600, // delay in seconds before rate limit key is reset
  // cache // memory store to use. Defaults to in-memory store
});

const app = express();
const port = 3000;

app.use(rateLimiter);
app.get("/", (req, res) => res.send("Hello World!"));
app.get("/test", (req, res) => res.send("Hello World!"));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Configuration options

maxRequests

Maximum number of requests an ip can make to a specific route. Defaults to 100.

timeFrame

How long - in seconds - before the number of requests an ip made is reset. Defaults to 3600s (1 hour)

cache

Where to store the number of requests and how expiry is handled

By default, the LocalCache is used. There is also a RedisCache included.

To create your own store, follow this interface

class CustomStore() {
  /**
   * @param {*} delay - how long before a key expires 
   */
  setDelay(delay) {}

  /**
   * @param {*} key - the key to store in the cache 
   */
  async incr(key){}
}

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.