forked from lfb/nodejs-koa-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (40 loc) · 1.06 KB
/
app.js
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
44
45
46
47
48
49
const Koa = require('koa')
const InitManager = require('./core/init')
const parser = require('koa-bodyparser')
const cors = require('@koa/cors');
const ratelimit = require('koa-ratelimit');
require('module-alias/register')
const catchError = require('./middlewares/exception')
const app = new Koa()
app.use(cors())
app.use(catchError)
app.use(parser())
// 接口调用频率限制(Rate-Limiting)
// Rate limiter middleware for koa.
// https://github.com/koajs/ratelimit
const db = new Map();
app.use(ratelimit({
driver: 'memory',
db: db,
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: 100,
disableHeader: false,
whitelist: (ctx) => {
// some logic that returns a boolean
},
blacklist: (ctx) => {
// some logic that returns a boolean
}
}));
InitManager.initCore(app)
app.listen(5000, () => {
console.log('Koa is listening in http://localhost:5000')
})
module.exports = app