/
app.ts
46 lines (36 loc) · 934 Bytes
/
app.ts
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
// /source/app.ts
// Create a basic server that uses express-rate-limit to rate limit requests
import createServer from 'express'
import rateLimit, {
MemoryStore,
Store,
IncrementResponse,
} from 'express-rate-limit'
class TestStore implements Store {
hits: Record<string, number> = {}
async increment(key: string): Promise<IncrementResponse> {
if (!this.hits[key]) this.hits[key] = 0
this.hits[key] += 1
return {
totalHits: this.hits[key],
resetTime: undefined,
}
}
async decrement(key: string): Promise<void> {
if (this.hits[key]) this.hits[key]--
}
async resetKey(key: string): Promise<void> {
delete this.hits[key]
}
}
const app = createServer()
app.use(
rateLimit({
max: 2,
legacyHeaders: false,
standardHeaders: true,
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
}),
)
app.get('/', (request, response) => response.send('Hello!'))
export default app