A simple rate-limiter middleware built with Node.js & MongoDB.
This page demos the functionalities of the rate-limiter middleware.
You're allowed to GET /api/rateLimiter 1000 times per hour.
If you do not excess the remaining amount, you'll see the message:
Your IP is {your_ip_address} which remains {remaining} connections in {X} mins {Y} secs.
Otherwise (too many requeset),
Request failed with status code 429
| Method | URL | Operation |
|---|---|---|
GET |
/api/rateLimiter | Fetch the rateLimiter page |
GET |
/api/visitors | Fetch all visitors |
Every time you
- land
/ - refresh
/ - or
GET /api/rateLimiter
the React component will invoke following function fetchRateLimiter()
fetchRateLimiter = async () => {
try {
const res = await axios.get('/api/rateLimiter');
const ip = res.headers['ip'];
const remaining = Number(res.headers['x-ratelimit-remaining']);
const reset = Number(res.headers['x-ratelimit-reset']);
this.setState({ ip, remaining, reset, err: false, success: true });
} catch (err) {
this.setState({ err: err.message, success: false });
}
};All configs can be found in /config
In /config/index.js:
ttlmeans time to live andmaxspecifies the limits.
After setting your MongoDB Atlas cluster, type following in your /config/dev.js:
module.exports = {
mongoURI:
'mongodb+srv://{username}:{password}@{database_name}.mongodb.net/test?retryWrites=true'
};npm i && npm i --prefix client # install dependencies
npm run dev # run the development environment