Skip to content

Express Middleware

Roman edited this page Oct 29, 2018 · 15 revisions

Express Middleware

const rateLimiter = new RateLimiterMemory({
  points: 5, // 5 requests
  duration: 1, // per second
});

const rateLimiterMiddleware = (req, res, next) => {
  rateLimiter.consume(req.connection.remoteAddress)
    .then(() => {
      next();
    })
    .catch((rejRes) => {
      res.status(429).send('Too Many Requests');
    });
};

See all options here