Skip to content

Commit dede67c

Browse files
committed
adding rate-limit docs
1 parent cd8f223 commit dede67c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,38 @@ By using the `proxyHandler` hook, developers can optionally intercept and modify
153153

154154
Please see the `demos/circuitbreaker.js` example for more details using the `opossum` library.
155155

156+
## Rate Limiting
157+
[Rate limiting](https://en.wikipedia.org/wiki/Rate_limiting), as well many other gateway level features can be easily implemented using `fast-gateway`:
158+
```js
159+
const rateLimit = require('express-rate-limit')
160+
const requestIp = require('request-ip')
161+
162+
gateway({
163+
middlewares: [
164+
// first acquire request IP
165+
(req, res, next) => {
166+
req.ip = requestIp.getClientIp(req)
167+
return next()
168+
},
169+
// second enable rate limiter
170+
rateLimit({
171+
windowMs: 1 * 60 * 1000, // 1 minutes
172+
max: 60, // limit each IP to 60 requests per windowMs
173+
handler: (req, res) => res.send('Too many requests, please try again later.', 429)
174+
})
175+
],
176+
177+
// your downstream services
178+
routes: [{
179+
prefix: '/public',
180+
target: 'http://localhost:3000'
181+
}, {
182+
// ...
183+
}]
184+
})
185+
```
186+
> In this example we have used the [express-rate-limit](https://www.npmjs.com/package/express-rate-limit) module.
187+
156188
## Gateway level caching
157189
Caching support is provided by the `http-cache-middleware` module. https://www.npmjs.com/package/http-cache-middleware
158190

0 commit comments

Comments
 (0)