1.0.11 • Published 1 year ago

@k_ankit/rate-limit-middleware v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Rate Limit Middleware

Rate Limiting Middleware for Express

Installation

$ npm install --save @k_ankit/rate-limit-middleware

Config

FieldTypeDefaultDescription
timeFrameInMsNumber10000Time window in milliseconds
maxHitsNumber10Maximum number of requests allowed within the time window
clientIdentifierHeaderStringIdentifier Header for the client
clientIdentifierExtracterFunctionExtract custom identifiers for clients
messageStringToo Many RequestsResponse body if limit is exceeded
statusCodeNumber429Status Code if limit is exceeded
prefixStringPrefix to append to the Client Identifier
storeStringIN_MEMORYStore to be used to store the data, Allowed: 'IN_MEMORY', 'REDIS'
redisConnectionConfigObjectStore Client Config to be used to store the data, Must be provided if REDIS store is used

Note: If clientIdentifierHeader and clientIdentifierExtracter are not provided Ip address is used as client identifier

Usage

Basic Example

The below code uses IN_MEMORY store.

import { createRateLimiter } from '@k_ankit/rate-limit-middleware'
import Express from 'express'

const config = {
  timeFrameInMs: 10000,
  maxHits: 10,
  clientIdentifierHeader: 'x-token'
}

const rateLimiter = await createRateLimiter(config)

const TestRouter = new Express.Router()
TestRouter.get('/test', rateLimiter)

The below code uses REDIS store.

import { createRateLimiter } from '@k_ankit/rate-limit-middleware'
import Express from 'express'

const config = {
  timeFrameInMs: 10000,
  maxHits: 10,
  clientIdentifierHeader: 'x-token',
  store: 'REDIS',
  redisConnectionConfig: {
    host: 'sample.redis.server',
    port: '6379',
    user: 'your_user_here',
    password: 'your_password_here'
  }
}

const rateLimiter = await createRateLimiter(config)

const TestRouter = new Express.Router()
TestRouter.get('/test', rateLimiter)
1.0.11

1 year ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago