{"id":13500236,"url":"https://github.com/koajs/ratelimit","last_synced_at":"2025-05-15T16:04:39.800Z","repository":{"id":39633124,"uuid":"14376847","full_name":"koajs/ratelimit","owner":"koajs","description":"Rate limiter middleware","archived":false,"fork":false,"pushed_at":"2024-01-29T14:49:47.000Z","size":72,"stargazers_count":490,"open_issues_count":3,"forks_count":56,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-11T13:06:35.301Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2013-11-13T21:23:46.000Z","updated_at":"2025-05-04T15:25:11.000Z","dependencies_parsed_at":"2023-09-26T20:42:57.341Z","dependency_job_id":"e754fc9a-ed8d-46a9-a390-e6117176fe91","html_url":"https://github.com/koajs/ratelimit","commit_stats":{"total_commits":89,"total_committers":30,"mean_commits":2.966666666666667,"dds":0.8314606741573034,"last_synced_commit":"ff212ea1f1e935b933c277b4fefe1369f66ffc93"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fratelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fratelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fratelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fratelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/ratelimit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253727056,"owners_count":21954131,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:00:53.934Z","updated_at":"2025-05-15T16:04:39.759Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"# koa-ratelimit\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![node version][node-image]][node-url]\n\n\nRate limiter middleware for koa.\n\n\n## Installation\n\n```bash\n# npm\n$ npm install koa-ratelimit\n# yarn\n$ yarn add koa-ratelimit\n```\n\n\n## Example\n\n### With a Redis driver\n\n```js\nconst Koa = require('koa');\nconst ratelimit = require('koa-ratelimit');\nconst Redis = require('ioredis');\nconst app = new Koa();\n\n// apply rate limit\napp.use(ratelimit({\n  driver: 'redis',\n  db: new Redis(),\n  duration: 60000,\n  errorMessage: 'Sometimes You Just Have to Slow Down.',\n  id: (ctx) =\u003e ctx.ip,\n  headers: {\n    remaining: 'Rate-Limit-Remaining',\n    reset: 'Rate-Limit-Reset',\n    total: 'Rate-Limit-Total'\n  },\n  max: 100,\n  disableHeader: false,\n  whitelist: (ctx) =\u003e {\n    // some logic that returns a boolean\n  },\n  blacklist: (ctx) =\u003e {\n    // some logic that returns a boolean\n  }\n}));\n\n// response middleware\napp.use(async (ctx) =\u003e {\n  ctx.body = 'Stuff!';\n});\n\n// run server\napp.listen(\n  3000,\n  () =\u003e console.log('listening on port 3000')\n);\n```\n\n### With a memory driver\n\n```js\nconst Koa = require('koa');\nconst ratelimit = require('koa-ratelimit');\nconst app = new Koa();\n\n// apply rate limit\nconst db = new Map();\n\napp.use(ratelimit({\n  driver: 'memory',\n  db: db,\n  duration: 60000,\n  errorMessage: 'Sometimes You Just Have to Slow Down.',\n  id: (ctx) =\u003e ctx.ip,\n  headers: {\n    remaining: 'Rate-Limit-Remaining',\n    reset: 'Rate-Limit-Reset',\n    total: 'Rate-Limit-Total'\n  },\n  max: 100,\n  disableHeader: false,\n  whitelist: (ctx) =\u003e {\n    // some logic that returns a boolean\n  },\n  blacklist: (ctx) =\u003e {\n    // some logic that returns a boolean\n  }\n}));\n\n// response middleware\napp.use(async (ctx) =\u003e {\n  ctx.body = 'Stuff!';\n});\n\n// run server\napp.listen(\n  3000,\n  () =\u003e console.log('listening on port 3000')\n);\n```\n\n\n## Options\n\n  - `driver` memory or redis [redis]\n  - `db` redis connection instance or Map instance (memory)\n  - `duration` of limit in milliseconds [3600000]\n  - `errorMessage` custom error message\n  - `id` id to compare requests [ip]\n  - `namespace` prefix for storage driver key name [limit]\n  - `headers` custom header names\n  - `max` max requests within `duration` [2500]\n  - `disableHeader` set whether send the `remaining, reset, total` headers [false]\n  - `remaining` remaining number of requests [`'X-RateLimit-Remaining'`]\n  - `reset` reset timestamp [`'X-RateLimit-Reset'`]\n  - `total` total number of requests [`'X-RateLimit-Limit'`]\n  - `whitelist` if function returns true, middleware exits before limiting\n  - `blacklist` if function returns true, `403` error is thrown\n  - `throw` call ctx.throw if true\n\n\n## Responses\n\nExample 200 with header fields:\n\n```\nHTTP/1.1 200 OK\nX-Powered-By: koa\nX-RateLimit-Limit: 100\nX-RateLimit-Remaining: 99\nX-RateLimit-Reset: 1384377793\nContent-Type: text/plain; charset=utf-8\nContent-Length: 6\nDate: Wed, 13 Nov 2013 21:22:13 GMT\nConnection: keep-alive\n\nStuff!\n```\n\nExample 429 response:\n\n```\nHTTP/1.1 429 Too Many Requests\nX-Powered-By: koa\nX-RateLimit-Limit: 100\nX-RateLimit-Remaining: 0\nX-RateLimit-Reset: 1384377716\nContent-Type: text/plain; charset=utf-8\nContent-Length: 39\nRetry-After: 7\nDate: Wed, 13 Nov 2013 21:21:48 GMT\nConnection: keep-alive\n\nRate limit exceeded, retry in 8 seconds\n```\n\n\n## License\n\n[MIT](LICENSE)\n\n\n##\n\n[npm-image]: https://img.shields.io/npm/v/koa-ratelimit.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-ratelimit\n[travis-image]: https://img.shields.io/travis/koajs/ratelimit.svg?style=flat-square\n[travis-url]: https://travis-ci.org/koajs/ratelimit\n[node-image]: https://img.shields.io/badge/node.js-%3E=_10-green.svg?style=flat-square\n[node-url]: http://nodejs.org/download/\n","funding_links":[],"categories":["Middleware","JavaScript","Libraries","仓库"],"sub_categories":["Rate Limit 🚦","中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fratelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fratelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fratelimit/lists"}