express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-limiter vs express-brute
Node.js 請求限制套件
express-rate-limitrate-limiter-flexibleexpress-slow-downexpress-limiterexpress-brute

Node.js 請求限制套件

這些套件主要用於限制進入 Node.js 應用程式的請求數量,以防止濫用和攻擊。它們提供了不同的策略和功能來控制請求速率,確保應用程式的穩定性和安全性。使用這些套件可以有效地保護伺服器資源,避免過載,並提升使用者體驗。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
express-rate-limit99,847,9223,317158 kB94 小時前MIT
rate-limiter-flexible4,759,0733,606234 kB123 天前ISC
express-slow-down190,03430338.7 kB43 個月前MIT
express-limiter15,951422-219 年前MIT
express-brute14,858565-2110 年前BSD

功能比較: express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-limiter vs express-brute

功能

  • express-rate-limit:

    提供豐富的配置選項,如限制請求數量、時間窗口、錯誤處理等,易於使用。

  • rate-limiter-flexible:

    高性能請求限制,支持多種儲存後端,並提供靈活的配置選項,適合高流量應用。

  • express-slow-down:

    通過減慢響應時間來防止過載,支持多種配置選項,如減慢比例和延遲時間。

  • express-limiter:

    輕量級的請求限制,支持簡單的配置,適合快速集成。

  • express-brute:

    提供簡單的暴力破解防護,支持多種儲存後端(如記憶體、Redis等),並可自定義限制邏輯。

擴展性

  • express-rate-limit:

    提供中等擴展性,支持自定義限制邏輯,但不如 express-brute 靈活。

  • rate-limiter-flexible:

    擴展性極高,支持多種後端和複雜的限制邏輯,適合大型應用。

  • express-slow-down:

    擴展性有限,主要用於減慢響應速度,功能較為單一。

  • express-limiter:

    擴展性有限,主要針對基本的請求限制功能。

  • express-brute:

    可擴展性強,支持自定義儲存和限制邏輯,適合需要特定需求的應用。

學習曲線

  • express-rate-limit:

    學習曲線適中,配置選項較多,但文檔清晰易懂。

  • rate-limiter-flexible:

    學習曲線較高,因為功能強大且配置靈活,需要一定的理解。

  • express-slow-down:

    學習曲線低,簡單易用,適合快速實現。

  • express-limiter:

    學習曲線非常低,快速集成和使用。

  • express-brute:

    學習曲線平緩,易於上手,適合初學者使用。

性能

  • express-rate-limit:

    性能穩定,適合大多數應用,但在極端高流量下可能需要優化。

  • rate-limiter-flexible:

    性能優越,能夠處理高流量請求,並且支持多種優化策略。

  • express-slow-down:

    性能會受到影響,因為它會故意減慢響應速度。

  • express-limiter:

    性能優越,適合快速響應的應用。

  • express-brute:

    性能良好,適合中小型應用,但在高流量情況下可能會受到影響。

使用場景

  • express-rate-limit:

    適合需要全面請求限制的應用,如網站或服務。

  • rate-limiter-flexible:

    適合需要高性能和靈活配置的應用,如大型商業應用。

  • express-slow-down:

    適合需要減少伺服器負載的應用,如高流量網站。

  • express-limiter:

    適合需要基本請求限制的應用,如 API。

  • express-brute:

    適合需要防止暴力破解的應用,如登入系統。

如何選擇: express-rate-limit vs rate-limiter-flexible vs express-slow-down vs express-limiter vs express-brute

  • express-rate-limit:

    如果你需要一個功能全面且易於使用的請求限制器,並且希望能夠輕鬆配置限制條件,express-rate-limit 是不錯的選擇。

  • rate-limiter-flexible:

    如果你需要一個高性能且支持多種儲存後端的請求限制解決方案,並且希望能夠靈活配置限制邏輯,rate-limiter-flexible 是最佳選擇。

  • express-slow-down:

    當你需要在請求過多時減慢響應速度以保護伺服器時,express-slow-down 是理想的選擇。它可以有效地減少伺服器的負載。

  • express-limiter:

    選擇 express-limiter 如果你需要一個輕量級的請求限制器,並且希望能夠快速集成到現有的 Express 應用程式中。

  • express-brute:

    如果你需要一個簡單且可擴展的解決方案來防止暴力破解攻擊,並且希望能夠自定義儲存策略,選擇 express-brute 是合適的。

express-rate-limit 的 README

express-rate-limit

tests npm version npm downloads inspect.software 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.
loggerLoggerCustom logger

Thank You


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

Create your docs today


And 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