express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-brute vs express-limiter
Node.js 速率限制库
express-rate-limitrate-limiter-flexibleexpress-slow-downexpress-bruteexpress-limiter类似的npm包:
Node.js 速率限制库

这些库用于在 Node.js 应用程序中实现速率限制,以防止滥用和保护资源。它们通过限制特定时间内的请求数量来帮助开发者控制流量,确保系统的稳定性和安全性。每个库都有其独特的功能和使用场景,适合不同的需求。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
express-rate-limit9,597,1893,194141 kB81 个月前MIT
rate-limiter-flexible1,307,1863,437191 kB164 天前ISC
express-slow-down58,40629238.2 kB01 个月前MIT
express-brute13,153568-219 年前BSD
express-limiter12,156423-218 年前MIT
功能对比: express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-brute vs express-limiter

存储选项

  • express-rate-limit:

    支持内存和 Redis 存储,适合中大型应用,能够处理分布式请求。

  • rate-limiter-flexible:

    支持多种存储选项,包括 Redis、MongoDB 和内存,适合复杂的应用需求。

  • express-slow-down:

    使用内存存储,适合简单的应用场景,能够快速集成。

  • express-brute:

    支持多种存储后端,如内存、Redis 和 MongoDB,允许开发者根据需求选择合适的存储方式。

  • express-limiter:

    主要使用内存存储,适合小型应用,但不适合分布式环境。

灵活性与配置

  • express-rate-limit:

    支持多种配置选项,如限制次数、时间窗口和响应格式,适合大多数应用场景。

  • rate-limiter-flexible:

    提供丰富的配置选项,支持自定义限制逻辑,适合复杂的业务需求。

  • express-slow-down:

    允许开发者自定义响应延迟时间,适合需要动态调整的场景。

  • express-brute:

    提供灵活的配置选项,允许开发者自定义限制策略和响应行为,适合需要高度定制的应用。

  • express-limiter:

    配置简单,适合快速集成,但灵活性较低。

易用性

  • express-rate-limit:

    文档完善,易于上手,适合大多数开发者。

  • rate-limiter-flexible:

    虽然功能强大,但配置相对复杂,适合有经验的开发者。

  • express-slow-down:

    简单易用,能够快速实现请求延迟,适合快速开发。

  • express-brute:

    易于集成和使用,适合初学者和小型项目。

  • express-limiter:

    非常轻量,几乎不需要配置,适合快速开发。

性能

  • express-rate-limit:

    经过优化,能够处理较高的请求量,适合中大型应用。

  • rate-limiter-flexible:

    性能优秀,能够处理高并发请求,适合复杂的应用场景。

  • express-slow-down:

    在请求过于频繁时会降低响应速度,适合防止恶意请求。

  • express-brute:

    在高并发情况下可能会受到内存限制的影响,适合小型应用。

  • express-limiter:

    性能较好,但在高流量环境下可能会出现瓶颈。

社区支持

  • express-rate-limit:

    广泛使用,社区支持强大,文档丰富。

  • rate-limiter-flexible:

    社区活跃,功能强大,适合复杂需求。

  • express-slow-down:

    社区支持一般,适合简单场景。

  • express-brute:

    社区活跃,文档清晰,适合初学者。

  • express-limiter:

    社区较小,但基本功能足够使用。

如何选择: express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-brute vs express-limiter
  • express-rate-limit:

    选择 express-rate-limit 如果你需要一个功能全面的速率限制库,支持内存存储和 Redis 存储,并且希望能够轻松地设置限制和响应处理。

  • rate-limiter-flexible:

    选择 rate-limiter-flexible 如果你需要一个高度灵活的速率限制库,支持多种存储选项(如 Redis、MongoDB 等),并且希望能够自定义限制逻辑。

  • express-slow-down:

    选择 express-slow-down 如果你希望在请求过于频繁时降低响应速度,而不是直接拒绝请求,这样可以有效防止恶意攻击。

  • express-brute:

    选择 express-brute 如果你需要一个简单易用的速率限制解决方案,支持多种存储后端,并且希望能够灵活配置限制策略。

  • express-limiter:

    选择 express-limiter 如果你需要一个轻量级的速率限制库,能够快速集成并且不需要复杂的配置。

express-rate-limit的README

express-rate-limit

tests npm version npm downloads license

Basic rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset. Plays nice with express-slow-down and ratelimit-header-parser.

Usage

The full documentation is available on-line.

import { rateLimit } from 'express-rate-limit'

const limiter = rateLimit({
	windowMs: 15 * 60 * 1000, // 15 minutes
	limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
	standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
	legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
	ipv6Subnet: 56, // Set to 60 or 64 to be less aggressive, or 52 or 48 to be more aggressive
	// store: ... , // Redis, Memcached, etc. See below.
})

// Apply the rate limiting middleware to all requests.
app.use(limiter)

Data Stores

The rate limiter comes with a built-in memory store, and supports a variety of external data stores.

Configuration

All function options may be async. Click the name for additional info and default values.

OptionTypeRemarks
windowMsnumberHow long to remember requests for, in milliseconds.
limitnumber | functionHow many requests to allow.
messagestring | json | functionResponse to return after limit is reached.
statusCodenumberHTTP status code after limit is reached (default is 429).
handlerfunctionFunction to run after limit is reached (overrides message and statusCode settings, if set).
legacyHeadersbooleanEnable the X-Rate-Limit header.
standardHeaders'draft-6' | 'draft-7' | 'draft-8'Enable the Ratelimit header.
identifierstring | functionName associated with the quota policy enforced by this rate limiter.
storeStoreUse a custom store to share hit counts across multiple nodes.
passOnStoreErrorbooleanAllow (true) or block (false, default) traffic if the store becomes unavailable.
keyGeneratorfunctionIdentify users (defaults to IP address).
ipv6Subnetnumber (32-64) | function | falseHow many bits of IPv6 addresses to use in default keyGenerator
requestPropertyNamestringAdd rate limit info to the req object.
skipfunctionReturn true to bypass the limiter for the given request.
skipSuccessfulRequestsbooleanUncount 1xx/2xx/3xx responses.
skipFailedRequestsbooleanUncount 4xx/5xx responses.
requestWasSuccessfulfunctionUsed by skipSuccessfulRequests and skipFailedRequests.
validateboolean | objectEnable or disable built-in validation checks.

Thank You

Sponsored by Zuplo a fully-managed API Gateway for developers. Add dynamic rate-limiting, authentication and more to any API in minutes. Learn more at zuplo.com

zuplo-logo


Thanks to Mintlify for hosting the documentation at express-rate-limit.mintlify.app

Create your docs today


Finally, thank you to everyone who's contributed to this project in any way! 🫶

Issues and Contributing

If you encounter a bug or want to see something added/changed, please go ahead and open an issue! If you need help with something, feel free to start a discussion!

If you wish to contribute to the library, thanks! First, please read the contributing guide. Then you can pick up any issue and fix/implement it!

License

MIT © Nathan Friedly, Vedant K