The configuration options for the rate limit plugin
An Elysia plugin that adds rate limiting functionality
// Create Redis instance
const redis = new Redis({
host: 'localhost',
port: 6379
});
await redis.connect();
// Create and configure the application with rate limiting
const app = new Elysia()
.use(rateLimit({
store: redis,
limit: 100, // 100 requests
window: 60, // per minute
}))
.get('/public-api', () => {
return { success: true, message: 'This endpoint is rate limited' };
});
// Start the server
app.listen(3000);
The
rateLimitPlugin
provides rate limiting capabilities for Elysia applications, protecting APIs from excessive use and potential abuse. It tracks request rates by client IP and enforces configurable limits based on a sliding time window.Rate Limit Headers:
The plugin adds the following headers to all responses:
X-RateLimit-Limit
: The maximum number of requests allowed in the windowX-RateLimit-Remaining
: The number of requests remaining in the current windowX-RateLimit-Reset
: The time in seconds until the rate limit resets