express.js-ratelimiter

1.0.4 • Public • Published

express-ratelimiter

This project is not affiliated with "express" (Not official)

Work In Progress, you shouldn't confidently use this in production. (It does work though)

How to use (simple example):

const express = require('express');
 
const app = express();
 
const IndexRoute = require('./routes/IndexRoute');
const indexRoute = new IndexRoute(
    {
        timePassedBetween: 1000, // 1000ms, the min time passed between two requests. DEFAULT: 50ms
        maxPerMinute: 3 // max amount of requests per minute. DEFAULT: Number.MAX_SAFE_INTEGER
    });
 
app.get('/', (req, res, next) =>
{
    indexRoute.preHandle(req, res, next);
});
 
app.listen(3000);

IndexRoute.js:

const { ExpressRoute } = require('express.js-ratelimiter');
 
class IndexRoute extends ExpressRoute
{
    handle(req, res, next)
    {
        res.send('Hello World');
    }
}
 
module.exports = IndexRoute;

To change the responses to blocked requests override the ExpressRoute.requestBlocked method. Current form:

requestBlocked(res)
{
    res.status(429).json(
        {
            statusCode: 429,
            errorCode: 'TO_MANY_REQUESTS',
            error: 'You have been rate-limited. Calm down and try again later.'
        });
}

Package Sidebar

Install

npm i express.js-ratelimiter

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

6 kB

Total Files

6

Last publish

Collaborators

  • fabosch