Skip to content

Koa Middleware

Roman edited this page Oct 21, 2018 · 12 revisions

Koa Middleware

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

app.use(async (ctx, next) => {
  try {
    await rateLimiter.consume(ctx.ip)
    next()
  } catch (rejRes) {
    ctx.status = 429
    ctx.body = 'Too Many Requests'
  }
})

See all options here