#middleware

22 snippets tagged with #middleware

typescriptintermediate

JWT Verify Middleware

Express middleware that verifies JWT tokens from the Authorization header and attaches the decoded payload to the request.

Best for: REST API authentication

#jwt#express
typescriptintermediate

In-Memory Rate Limiter for Express

Token bucket rate limiter middleware for Express with configurable window and max requests per IP.

Best for: API abuse prevention

#express#rate-limit
typescriptbeginner

Async Error Handler Wrapper

Higher-order function that wraps async Express route handlers and forwards rejected promises to error middleware.

Best for: Express route error handling

#express#async
typescriptbeginner

Express Zod Request Validation

Validate Express request body, params, and query with Zod schemas via reusable middleware.

Best for: API input validation

#express#zod
typescriptintermediate

Middleware Pattern and Composition

Implement the middleware pattern for composable request processing with next() and error handling.

Best for: Request processing pipelines

#nodejs#middleware
typescriptintermediate

Express Error Handling Middleware

Centralized error handling in Express with custom error classes, async wrapper, and structured responses.

Best for: API error standardization

#nodejs#express
typescriptintermediate

Middleware Chain Pattern

Implement the middleware/pipeline pattern for composable request processing without Express.

Best for: Custom HTTP framework

#nodejs#middleware
typescriptbeginner

CORS Configuration Middleware

Configure Cross-Origin Resource Sharing with origin allowlists, credentials, and preflight handling.

Best for: API CORS configuration

#nodejs#cors
typescriptintermediate

Request ID Tracing Middleware

Express middleware that generates or forwards X-Request-Id headers for distributed tracing.

Best for: Distributed tracing

#express#tracing
typescriptintermediate

IP Geolocation Middleware

Extracts client IP and attaches geolocation data to the request using a lightweight lookup.

Best for: Content localization

#express#geolocation
typescriptbeginner

Request Timeout Middleware

Express middleware that aborts requests exceeding a configurable time limit with 408 status.

Best for: Preventing hanging requests

#express#timeout
typescriptbeginner

Structured Request Logger Middleware

Express middleware that logs request/response details as structured JSON with timing information.

Best for: API monitoring

#express#logging
typescriptintermediate

In-Memory Response Cache

Simple TTL-based in-memory cache middleware for GET endpoints that serves cached responses.

Best for: Caching expensive queries

#cache#performance
typescriptintermediate

Authentication Middleware Guard

Next.js middleware that checks auth tokens on protected routes and redirects unauthenticated users to login.

Best for: Dashboard access control

#middleware#authentication
typescriptintermediate

Edge Middleware Geolocation

Use Vercel Edge geolocation headers to personalize content based on the visitor's country and city.

Best for: GDPR consent flows

#edge#geolocation
typescriptadvanced

Edge Middleware Rate Limiter

Rate limit API requests at the edge using a sliding window counter with configurable thresholds.

Best for: API abuse prevention

#middleware#rate-limiting
typescriptadvanced

Next.js Middleware for Authentication

Protect routes with Next.js middleware using token verification and role-based redirects.

Best for: Protecting authenticated routes at the edge

#nextjs#middleware
typescriptadvanced

Middleware Chain Pattern

Compose multiple middleware functions for auth, rate limiting, and geolocation in Next.js middleware.

Best for: Auth guard middleware

#nextjs#middleware
typescriptadvanced

Middleware Geo-based Redirect

Redirect users based on their geographic location using Next.js edge middleware.

Best for: internationalization

#nextjs#middleware
typescriptadvanced

Authentication Middleware with Session Check

Protect routes with middleware that validates session tokens and redirects unauthenticated users.

Best for: route protection

#nextjs#auth
typescriptadvanced

Rate Limiter for Edge Functions

Implement sliding window rate limiting in Next.js middleware using in-memory or KV store.

Best for: API protection

#nextjs#rate-limiting
typescriptadvanced

Multi-Tenant App with Subdomains

Build a multi-tenant application using middleware to resolve tenant from subdomain.

Best for: SaaS platforms

#nextjs#multi-tenant