koa-ratelimit访问频率限制

  |  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const Koa = require('koa');
const ratelimit = require('koa-ratelimit');
const Redis = require('ioredis');
const app = new Koa();

app.use(ratelimit({
'driver': 'redis',
'db': new Redis({
'max_clients': 10,
'min_clients': 1,
'port': 6379,
'host': '127.0.0.1',
'password': '1234567',
}),
'duration': 60000,
'errorMessage': 'Sometimes You Just Have to Slow Down.',
'id': ctx => ctx.ip,
'headers': {
'remaining': 'Rate-Limit-Remaining',
'reset': 'Rate-Limit-Reset',
'total': 'Rate-Limit-Total',
},
'max': 5, // 60秒内最大访问次数5
'disableHeader': false,
'whitelist': ctx => {
// console.log('whitelist===========>', ctx);
// some logic that returns a boolean
},
'blacklist': ctx => {
// console.log('blacklist===========>', ctx);
// some logic that returns a boolean
},
}));


app.use(async ctx => {
ctx.body = 'Stuff!';
});

app.listen(3000);
console.log('listening on port 3000');

访问 http://127.0.0.1:3000
文章目录