1. Runtime Validation at the Edge with Zod
TypeScript types vanish at runtime during compilation. Never trust raw client input. Every incoming payload to an Express route handler must be validated against a strict Zod schema before invoking business logic.
import { z } from "zod";
export const contactSchema = z.object({
name: z.string().min(2, "Name must be at least 2 characters").max(100),
email: z.string().email("Please provide a valid email address"),
projectType: z.enum(["Full-Stack", "Backend API", "SaaS Dashboard", "Audit"]),
budget: z.string().min(1, "Please select an estimated budget"),
message: z.string().min(10, "Message must be at least 10 characters").max(3000),
honeypot: z.string().max(0, "Bot detected"),
});
2. Sliding-Window Rate Limiting
Prevent brute-force authentication attacks and denial-of-service spam on contact routes using `express-rate-limit` backed by Redis in distributed environments.3. Hardening Headers with Helmet
Enforce strict Content Security Policies (CSP), HSTS, no-sniff, and deny framing to prevent clickjacking and injection attacks.
app.use(helmet({
contentSecurityPolicy: true,
crossOriginEmbedderPolicy: true,
xFrameOptions: { action: "deny" }
}));
4. Strict CORS & Cookie Configurations
Always set explicit origin whitelists rather than wildcard `*` headers, and store authentication tokens inside `httpOnly`, `secure`, `sameSite="strict"` cookies to neutralize XSS credential theft.
Written by Sumit Kumar Garsa
Senior MERN Stack Architect & Creative Engineer building scalable web architectures and sub-100ms distributed systems globally.