ysocorp / koa2-ratelimit

Rate-limiting middleware for Koa2 ES6. Use to limit repeated requests to APIs and/or endpoints such as password reset.
MIT License
120 stars 37 forks source link

Parameterized Route not working #28

Closed iit2011081 closed 3 years ago

iit2011081 commented 4 years ago
const getUserLimiter = RateLimit.middleware({
  interval: 15*60*1000, // 15 minutes
  max: 100,
  prefixKey: 'get/user/:id' // to allow the bdd to Differentiate the endpoint 
});
// add route with getUserLimiter middleware
router.get('/user/:id', getUserLimiter, (ctx) => {
  // Do your job
});

It is not differentiating according to actual value of id. /user/1 and /user/2 should be treated as 2 different prefixKey.

jeanclaudeyalap commented 4 years ago

Quick fix : You can use the keyGenerator option like this :

const getUserLimiter = RateLimit.middleware({
  interval: 15*60*1000, // 15 minutes
  max: 100,
  keyGenerator: async (ctx) => `get/${ctx.req.url}`,
});
// add route with getUserLimiter middleware
router.get('/user/:id', getUserLimiter, (ctx) => {
  // Do your job
});
deni-begaj commented 2 years ago

@jeanclaudeyalap suggestion worked for me, thanks!