express-rate-limit increasing request limit

78 Views Asked by At

How can I increase the request limit if the user continues rate limiting?

My rate limiter:

const Limiter = rateLimit({
  windowMs: 10000,
  max: 5,
  standardHeaders: true,
  legacyHeaders: false,
  keyGenerator: function (req) { return req.ip; },
  message: async (req, res) => {res.render("429", {message: `IP ${req.ip} was rate limited.`}) }
})

I tried searching it at google but i did not find anything that could help me.

1

There are 1 best solutions below

0
Nathan Friedly On

If you want the higher limit to apply to all users, change max from 5 to a higher number, e.g. 10, 50, etc.

If you want the higher limit to apply to only some users, set max to a function that returns the correct value based on the request:

const Limiter = rateLimit({
  windowMs: 10000,
  max: function(req) {
    if (/* you want to give this user a higher limit */) {
      return 10;
    }
    return 5; // fallback for everyone else
  },
  standardHeaders: true,
  legacyHeaders: false,
  keyGenerator: function (req) { return req.ip; },
  message: async (req, res) => {res.render("429", {message: `IP ${req.ip} was rate limited.`}) }
})

Disclosure: I'm the author of express-rate-limit.