ip-rate-limiter
v1.0.1
Published
A rate-limiter inspired by Dcard's backend developer intern homework.
Downloads
3
Readme
ip-rate-limiter
An expressJS ip-rate-limit middleware built with TypeScript which inspired by Dcard's internship program.
Requirements
- [X] 限制每小時來自同一個 IP 的請求數量不得超過 1000
- [X] 在 response headers 中加入剩餘的請求數量 (X-RateLimit-Remaining) 以及 rate limit 歸零的時間 (X-RateLimit-Reset)
- [X] 如果超過限制的話就回傳 429 (Too Many Requests)
- [X] 使用 TypeScript + express + redis 完成
Quick Start
Install
$ npm install ip-rate-limiter
Basic Usage
import express, { Application, Request, Response, NextFunction } from 'express';
import rateLimiter from 'ip-rate-limiter';
import bodyParser from 'body-parser';
const app: Application = express();
app.use(bodyParser.json());
app.get('/', rateLimiter, (req: Request, res: Response) => {
res.send('test');
})
app.listen(5000, () => {
console.log('server running on port 5000');
})
Roadmap
- [ ] Flexible arguments
- [ ] Increase Unit Test Coverage
- [ ] Support for other Redis Client