Skip to content

Express rate limiter for apps using mongoose

License

Notifications You must be signed in to change notification settings

markkuhn/express-rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT License

GitHub repo size

Mongoose Express Rate Limiter

Express rate limiter for apps using mongoose

Installation

npm i @markkuhn/express-rate-limiter

Usage

var express = require('express');
var app = express();
var mongoose = require('mongoose');

var rateLimiter = require('@markkuhn/express-rate-limiter');

app.use(rateLimiter({
    requestsPerMinute: 100,
    identifier: function(req) {
        return req.headers['x-forwarded-to'];
    },
    onBlocked: function(req, res) {
        res.sendStatus(429);
        console.log('Too many requests');
    }
}));

API options

rateLimiter(options)
  • requestsPerMinute : Number amount of requests allowed per minute (default: 60).

  • identifier : Function returns an identifier for a request (default: req.headers['x-forwarded-to'] || req.connection.remoteAddress).

  • onBlocked : Function called when limit has been reached (default: res.sendStatus(429)).