Rate limit module
Module for limiting the number of requests in a given time period.
Install
$ npm i @nestlab/rate-limit
Configuration
Example
RateLimitModule.forRoot({
rateLimitMessage: 'You made a lot of requests. Try again later',
defaultTokenProvider: req => req.ip,
enableForEnvironments: ['production', 'staging']
});
Usage
@Controller()
export class AppController {
@RateLimit(2000)
@Get('endpoint1')
endpoint1(): string {
return 'ENDPOINT 1 RESPONSE';
}
@RateLimit(10000, 5)
@Get('endpoint2')
endpoint2(): string {
return 'ENDPOINT 2 RESPONSE';
}
@RateLimit(10000, 5, req => req.user.userId)
@Get('endpoint3')
endpoint3(): string {
return 'ENDPOINT 3 RESPONSE';
}
}
Enjoy!