• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

scttcper/koa-simple-ratelimit: Simple rate limiter for Koa.js v2 web framework

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

scttcper/koa-simple-ratelimit

开源软件地址:

https://github.com/scttcper/koa-simple-ratelimit

开源编程语言:

TypeScript 100.0%

开源软件介绍:

koa-simple-ratelimit

NPM version build status coverage

Rate limiter middleware for koa v2. Differs from koa-ratelimit by not depending on ratelimiter and using redis ttl (time to live) to handle expiration time remaining. This creates only one entry in redis instead of the three that node-ratelimiter does.

Installation

npm install koa-simple-ratelimit

Example

import Koa from 'koa';
import redis from 'redis';

import { ratelimit } from 'koa-simple-ratelimit';

const app = new Koa();

// Apply rate limit

app.use(ratelimit({
	db: redis.createClient(),
	duration: 60000,
	max: 100
}));

// Response middleware

app.use((ctx, next) => {
	ctx.body = 'Stuff!';
	return next();
});

app.listen(4000);
console.log('listening on port http://localhost:4000');

Options

  • db redis connection instance
  • max max requests within duration [2500]
  • duration of limit in milliseconds [3600000]
  • id id to compare requests [ip]
  • allowlist array of ids to allowlist
  • blocklist array of ids to blocklist
  • prefix redis key prefix ["limit"]

Responses

Example 200 with header fields:

HTTP/1.1 200 OK
X-Powered-By: koa
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 99
X-RateLimit-Reset: 1384377793
Content-Type: text/plain; charset=utf-8
Content-Length: 6
Date: Wed, 13 Nov 2013 21:22:13 GMT
Connection: keep-alive

Stuff!

Example 429 response:

HTTP/1.1 429 Too Many Requests
X-Powered-By: koa
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1384377716
Content-Type: text/plain; charset=utf-8
Content-Length: 39
Retry-After: 7
Date: Wed, 13 Nov 2013 21:21:48 GMT
Connection: keep-alive

Rate limit exceeded, retry in 8 seconds

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LnsooXD/koa-socket.io: Koa meets socket.io connected socket发布时间:2022-07-09
下一篇:
Lobos/qenya: mock server base on koa and react发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap