npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-request-rate-limit

v2.1.1

Published

An Express middleware to limit request rate.

Downloads

7

Readme

express-request-rate-limit

An Express middleware to limit request rate.

Features

  • Zero dependency
  • Custom way to generate clientId
  • Customizable response when reach the limit
  • Default smart timer to free memory, according to request rate
  • Able to specify QPS(Quest Per Second) to set proper timer interval

Implement Detail

The middleware use a plain object to record access history. In each object key-value pair, key is clientId, value is a list which contains access timestamp.

When a new request coming, if the list reaches the max limit, the request is not allowed to go further, and rise 429 Too Many Requests error by default.

The middleware stores history data in Node.js process memory. We start a timer to cleans the history data repeatedly.

Usage

Follow these steps to use:

  1. require('express-request-rate-limit') returns a middleware generator. The common options will be applied in all middleware generated by it.
  2. Call the generator function to build middleware, you can override the default options in this step.
  3. Use the middleware on your router to limit the request rate.

Basic Usage

For example, we need to limit all the login requests from normal user and administrator, according to different policies:

  1. For normal user, set client IP address and user ID to specify a key. Limit rate is 5 times per minute.
  2. For administrator, set client IP address to specify a key. Limit rate is 1 time every 5 seconds.

Both of them share the same error response message 'Please try a while later' with status code 429.

const router = express.Router()
const rateLimit = require('express-request-rate-limit')

const loginLimit = rateLimit({
  onLimit: (req, res) => res.status(429).json({msg:'Please try a while later'})
})

const userLoginLimit = loginLimit({
  limit: 5,
  ms: 1000 * 60,
  keyFn: req => {return req.ip + req.user.id}
})

const adminLoginLimit = loginLimit({
  limit: 1,
  ms: 1000 * 5,
  keyFn: req => req.ip
})

router.use('/user/login', userLoginLimit)
router.use('/admin/login', adminLoginLimit)

Debug Mode

If you want to inspect the history data, use debug mode:

const userLoginLimit = loginLimit({
  debug: true,
  name: 'user login',
  // ...
})

There may be multiple instances running together, we need to know which one the debug log belongs to. For convenience, we specify its name in options.

Specify QPS

Some APIs may experience a traffic surge. For example, the QPS suddenly rises from 0 to 1000. In this case, auto-tuning timer is too slow to detect the sudden change. So we should specify QPS to a higher value.

NOTE: the cleaner timer has a range from 5 seconds to 5 minutes. If QPS > 1000, timer interval is 5 seconds. If QPS < 17, timer interval is 5 minutes.