Really efficient Antiflood for you API
$ npm i antiflood
const Antiflood = require('antiflood'); const test = new Antiflood( (number)MAX_REQUESTS_COUNT, (number)FLOOD_RESET_TIME );(constants and variables names are based on the "Initialization" section)
test.feed(TOKEN)The constructor, accepts 2 numbers:
- MAX_REQUESTS_COUNT: How many requests can a user make in FLOOD_RESET_TIME?
- FLOOD_RESET_TIME: Time in seconds after user can make a new cycle of requests
The token is that identifying object that allows the flood to group users based on requests, so you can create various Antifloods based on the TOKEN object passed by parameter. Example: User A: makes 3 requests within 10 seconds, and is blocked User B (with another token): at the same time as User A makes 2 requests in 10 seconds, he is not blocked because he has another token.
const email_verification = new Antiflood(2, 30); // 2 request every 30 seconds email_verification.feed({user: 1}); // allowed email_verification.feed({user: 2}); // allowed email_verification.feed({user: 1}); // allowed email_verification.feed({user: 1}); // denied email_verification.feed({user: 2}); // allowed