Skip to content

How to Rate Limit Specific Routes in Cloudflare Workers using @hono-rate-limiter/cloudflare? #29

Answered by MathurAditya724
2820402 asked this question in Q&A
Discussion options

You must be logged in to vote

You can control which routes are affected by the rate limiter by applying it as middleware only to the specific routes you want. The rate limiter in Hono works like any other middleware, so instead of using app.use globally, you can attach it directly to specific routes or route groups.

For example, you can do something like this:

const app = new Hono<AppType>();

// Apply rate limiter only to /api/data and /api/user
app.get('/api/data', cloudflareRateLimiter<AppType>({
  rateLimitBinding: (c) => c.env.RATE_LIMITER,
  keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "",
}), (c) => {
  // Your route handler for /api/data
});

app.get('/api/user', cloudflareRateLimiter<AppType>({
  r…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@2820402
Comment options

Answer selected by 2820402
Comment options

You must be logged in to vote
2 replies
@MathurAditya724
Comment options

@Zerebokep
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants