MD. NAZMUL ISLAM
HomeServicesPortfolioCase StudiesBlogAboutContact
ResumeLoginSign Up
Protecting Node.js APIs With Rate Limiting
  1. Home
  2. Blog
  3. Protecting Node.js APIs With Rate Limiting
Back to Blog
SecurityNode.jsExpressAPIBackend

Protecting Node.js APIs With Rate Limiting

December 1, 20264 min read2 views0 comments

Learn how to protect your Node.js and Express APIs from brute-force attacks and abuse using robust rate limiting strategies.

Protecting Node.js APIs With Rate Limiting

When I build web applications and APIs for clients, performance and uptime are always at the top of the priority list. One of the most common vulnerabilities I see in backend systems is a total lack of request throttling. Without proper controls, a single malicious script or even an accidental infinite loop in a frontend app can flood your server with thousands of requests per second. This leads to API crashes, degraded user experience, and potential downtime.

Today, let us look at how you can secure your Node.js applications against abuse by implementing effective rate limiting using Express and Redis. As a full-stack developer based in Bangladesh, I often help clients scale their backends to handle unpredictable traffic spikes safely. Rate limiting is one of the easiest and most reliable defenses you can add to your stack.

Why Rate Limiting Matters for Web Apps

Rate limiting restricts the number of times a user or IP address can hit your API within a specific time window. Without it, your application becomes an easy target for several common attacks:

  • Brute-Force Attacks: Attackers can rapidly guess passwords or API tokens by trying millions of combinations.
  • Denial of Service (DoS): Simple scripts can exhaust your database connections and memory by overwhelming endpoints.
  • Resource Scraping: Bots can scrape your public data endpoints continuously, inflating your server and database costs.

By implementing rate limits, you ensure that legitimate users get fast responses while automated scrapers and attackers get blocked or slowed down.

Setting Up Express Rate Limit

The quickest way to add basic rate limiting to an Express app is by using the express-rate-limit package. Let us install it in your project:

npm install express-rate-limit

Next, configure it in your main server file. Here is a standard configuration that limits each IP to 100 requests per 15 minutes on general API routes:

const express = require('express');
const rateLimit = require('express-rate-limit');

const app = express();

// Define the limiter rule
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // Limit each IP to 100 requests per windowMs
  standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
  legacyHeaders: false, // Disable the `X-RateLimit-*` headers
  message: {
    status: 429,
    error: 'Too many requests, please try again later.'
  }
});

// Apply the rate limiter to all API requests
app.use('/api/', limiter);

app.get('/api/data', (req, res) => {
  res.json({ message: 'Secure data delivered successfully.' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Storing Rate Limit State With Redis

The basic memory store in express-rate-limit works well for single-server setups. However, if you deploy your Node.js app across multiple containers or serverless instances, in-memory counters will not sync. Each server instance will track its own count, rendering your limits ineffective.

To solve this, use Redis as a centralized store for your rate limiter. Install the Redis store package:

npm install rate-limit-redis redis

Here is how you connect it to your Express application:

const express = require('express');
const rateLimit = require('express-rate-limit');
const { RedisStore } = require('rate-limit-redis');
const { createClient } = require('redis');

const app = express();

// Create Redis client
const redisClient = createClient({
  url: process.env.REDIS_URL || 'redis://localhost:6379'
});

redisClient.connect().catch(console.error);

const redisLimiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 50,
  standardHeaders: true,
  legacyHeaders: false,
  store: new RedisStore({
    sendCommand: (...args) => redisClient.call(...args),
  }),
  message: {
    status: 429,
    error: 'Too many requests from this IP, please try again later.'
  }
});

app.use('/api/auth/', redisLimiter);

app.post('/api/auth/login', (req, res) => {
  res.json({ message: 'Login endpoint' });
});

app.listen(3000);

Best Practices for API Security

When building production APIs, keep these additional security principles in mind:

  • Strict Limits on Authentication: Endpoints like /login, /register, and /forgot-password need much stricter limits (e.g., 5 requests per 15 minutes) to stop brute-force attacks.
  • Trust Proxy Settings: If your app sits behind a reverse proxy like Nginx or Cloudflare, make sure to configure app.set('trust proxy', 1) so Express reads the real client IP instead of the proxy IP.
  • Informative Error Headers: Always return standard rate limit headers so legitimate frontend clients know when their quota resets.

Whether I am building a content platform like Goldmines Picture Website or a complex logistics tool like JobsiteX, protecting backend endpoints from abuse is a fundamental step I never skip.

Need help building secure, high-performance APIs or web applications for your business? Get in touch to discuss your project.

Tagged with

SecurityNode.jsExpressAPIBackend

Comments

Leave a comment

You must be signed in to comment.

MD. NAZMUL ISLAM

Full-Stack & React Native Developer

Send a project enquiry

Navigation

  • Home
  • About
  • Services
  • Case Studies
  • Blog
  • Contact
  • 한국어 (Korean)

Services

  • Web Development
  • Mobile App Development
  • Custom Software & SaaS
  • SEO & Marketing
  • Domain & Hosting

© 2026 MD. Nazmul Islam. All rights reserved.

Privacy Policy·Terms of Service