Skip to content

dotcypress/micro-ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status NPM Version js-standard-style

Micro Rate Limit

Rate-limiting middleware for micro.

Installation

$ npm install micro-ratelimit

Examples

const rateLimit = require('micro-ratelimit')

module.exports = rateLimit((req, res) => {
  return 'Hello world'
})
const rateLimit = require('micro-ratelimit')

// Limit example: 2 requests per 10 sec
module.exports = rateLimit({ window: 10000, limit: 2, headers: true }, (req, res) => {
  return 'Hello world'
})

API

Options

  • window: how long to keep records of requests in memory in ms (default: 1 second)
  • limit: max number of requests during window (default: 1)
  • keyGenerator: key generator function (req -> client id)
  • headers: send rate limit headers (default: false)

Default implementation of keyGenerator:

function keyGenerator (req) {
  return req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    req.connection.socket.remoteAddress
}

About

Rate-limiting middleware for micro

Resources

License

Stars

Watchers

Forks

Packages

No packages published