npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-rate-limiter

v1.3.1

Published

Rate limiter middleware for express applications

Downloads

3,322

Readme

Express Rate Limiter

npmlicense

Rate limiter middleware for express applications.

This limiter has two kinds of limits: an inner and outer limit. It limites based on user ip.

The inner limit is against hammering (e.g. only 3 calls allowed per second). The outer limit is for protecting against over-use. (e.g. max 60 times per two minutes).

The limits are currently held in memory. It's on my roadmap to abstract the use of this module, so other modules can be plugged in instead (e.g. redis).

Usage

Install

npm install express-rate-limiter --save

First, create a new Limiter;

var Limiter = require('express-rate-limiter');
var MemoryStore = require('express-rate-limiter/lib/memoryStore');
var limiter = new Limiter({ db : new MemoryStore() });

The memory store is a lightweight in memory cache. This can be replaced by any other database implementing store.js, for example the MemoryStore.

Afterwards, use the limiter to create an express middleware for the express methods you want to rate limit.

app.post('/', limiter.middleware(), function(req, res) {   

});

Anything written in the callback will now be rate limited.

#Default values

Default settings for the created middleware are as follows:

  • outerTimeLimit: 2 * 60 * 1000
    • default: 2 minutes
    • Time in milliseconds for the outer limit.
    • This will also be used as the cache expiration.
    • Mainly to prevent over-use: The lenient limiter.
  • outerLimit: 60
    • default: 60 attempts
    • Number of attemps for the outer limit.
  • innerTimeLimit: 1500
    • default: 1.5 seconds
    • Time in milliseconds for the inner limit.
    • Mainly to prevent hammering: The hardcore limiter.
  • innerLimit: 3
    • default: 3 attemps
    • Number of attempts for the inner limit.
  • headers: true
    • default: add headers
    • Send headers along with response.
  • limitOnError: true
    • default: true
    • When an error occurs when looking up the ip in the key store, limit the post if true.
  • db: undefined
    • default: No default value available.
    • Key Value store being used to rate limit.
  • pathLimiter: boolean
    • default: false
    • adds a path to the ip for the request limiter identifier. It allows to has differents limiters for each path in the application
  • path ''
    • default: empty value
    • if this value is passed, it will be the value that will be joined to the ip for the limiter identifier. If has not value, the value will be readed from the request. This option only will be applied if "pathLimiter" has true as value

They can be overwritten globally by passing them to the initiator. Properties that were not passed will automatically take default value.

var limiter = new Limiter({innerLimit: 5});

Settings can also be overwritten per middleware. When a setting is not passed through the initiator, it will revert to the setting specified in the ctor of the Limiter you're using. If you didn't pass that setting there either, it will use the default value instead.

app.post('/', limiter.middleware({innerLimit: 10, headers: false}), function(req, res) {   

});

When the limit has been reached, the actual method logic will not be executed, but instead a status 429 (Too many Requests) will be sent to the client.

#Headers Headers are automatically added to the response.

The available headers are:

  • X-RateLimit-Limit
  • X-RateLimit-Remaining
  • X-RateLimit-Reset
  • Retry-After (Only in case of a 429 response)

For X-RateLimit-headers, the outer limits are used as response values.

#Notes The framework relies on the fact that express' req.ip is correct. This might not always be the case, e.g. when running behind a proxy like NGINX or hosting your app on a platform like Heroku.

When this is the case, don't forget to initialize express using the following snippet:

app.enable('trust proxy');