Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.48 KB

README.md

File metadata and controls

48 lines (34 loc) · 1.48 KB

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