Backend Boilerplates javascript Intermediate

Express In-Memory Rate Limiter

Simple in-memory rate limiter for Express using a Map — no Redis needed for small apps.

javascript
const store = new Map();
module.exports = (limit = 100, windowMs = 60000) => (req, res, next) => {
  const key  = req.ip;
  const now  = Date.now();
  const data = store.get(key) || { count: 0, start: now };
  if (now - data.start > windowMs) { data.count = 0; data.start = now; }
  data.count++;
  store.set(key, data);
  if (data.count > limit) return res.status(429).json({ error: 'Too many requests' });
  next();
};

Prerequisites

  • express >= 4.x

Pro Tips

  • → Use Redis for distributed rate limiting.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
javascript
Level
Intermediate
Read time
3 min
Published
Aug 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.