For Workers & Pages, what is the name of the domain?
### What is the issue or error you're encountering
Workers Rate Limiting API not working
### What are the steps to reproduce the issue?
import { cloudflareRateLimiter } from "@hono-rate-limiter/cloudflare";
const app = new Hono();
const getClientIP = (c) => {
return c.req.header("cf-connecting-ip") ||
c.req.header("x-real-ip") ||
c.req.header("x-forwarded-for")?.split(',')[0] ||
c.req.raw.socket?.remoteAddress ||
"unknown";
};
app.get("/api/search",
cloudflareRateLimiter({
rateLimitBinding: (c) => c.env.SEARCH_RATE_LIMITER,
keyGenerator: getClientIP,
}),
cache({ cacheName: 'search-cache', ttl: 3600 }), // Caching: 1 hour
async (c) => {
let query = decodeURIComponent(c.req.query('query') || '');
if (query.length > 60) { // Input length restriction: max 50 characters
c.status(400);
return c.text("Query too long");
}
}
);
wrangler.json --
"unsafe": {
"bindings": [
{
"name": "COMMENTS_RATE_LIMITER",
"type": "ratelimit",
"namespace_id": "1001",
"simple": {
"limit": 3,
"period": 60
}
},
{
"name": "SEARCH_RATE_LIMITER",
"type": "ratelimit",
"namespace_id": "1002",
"simple": {
"limit": 20,
"period": 60
}
}
]
it works if defined globally like this –
app.use(“/api/*”,
cloudflareRateLimiter({
rateLimitBinding: (c) => c.env.COMMENTS_RATE_LIMITER,
keyGenerator: (c) => c.req.header(“cf-connecting-ip”) ?? “”,
}),
);
but does not work if specific rate limiters are applied directly to the /api/posts/comments
and /api/search
endpoints…