Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const rateLimit = require('express-rate-limit');
- const app = express();
- // Rate limiter middleware instances for different routes
- const limiterGeneral = rateLimit({
- windowMs: 15 * 60 * 1000, // 15 minutes
- max: 5,
- message: {
- error: 'Rate limit exceeded for general requests. Please try again later.'
- }
- });
- const limiterSpecial = rateLimit({
- windowMs: 60 * 60 * 1000, // 1 hour
- max: 2,
- message: {
- error: 'Rate limit exceeded for special requests. Please try again later.'
- }
- });
- // Apply rate limiters to specific routes
- app.get('/', limiterGeneral, (req, res) => {
- res.send('Hello, this is a general rate-limited endpoint.');
- });
- app.get('/special', limiterSpecial, (req, res) => {
- res.send('Hello, this is a special rate-limited endpoint.');
- });
- // Start the server
- const PORT = process.env.PORT || 3000;
- app.listen(PORT, () => {
- console.log(`Server is running on port ${PORT}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement