#middleware
22 snippets tagged with #middleware
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
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
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 Zod Request Validation
Validate Express request body, params, and query with Zod schemas via reusable middleware.
Best for: API input validation
Middleware Pattern and Composition
Implement the middleware pattern for composable request processing with next() and error handling.
Best for: Request processing pipelines
Express Error Handling Middleware
Centralized error handling in Express with custom error classes, async wrapper, and structured responses.
Best for: API error standardization
Middleware Chain Pattern
Implement the middleware/pipeline pattern for composable request processing without Express.
Best for: Custom HTTP framework
CORS Configuration Middleware
Configure Cross-Origin Resource Sharing with origin allowlists, credentials, and preflight handling.
Best for: API CORS configuration
Request ID Tracing Middleware
Express middleware that generates or forwards X-Request-Id headers for distributed tracing.
Best for: Distributed tracing
IP Geolocation Middleware
Extracts client IP and attaches geolocation data to the request using a lightweight lookup.
Best for: Content localization
Request Timeout Middleware
Express middleware that aborts requests exceeding a configurable time limit with 408 status.
Best for: Preventing hanging requests
Structured Request Logger Middleware
Express middleware that logs request/response details as structured JSON with timing information.
Best for: API monitoring
In-Memory Response Cache
Simple TTL-based in-memory cache middleware for GET endpoints that serves cached responses.
Best for: Caching expensive queries
Authentication Middleware Guard
Next.js middleware that checks auth tokens on protected routes and redirects unauthenticated users to login.
Best for: Dashboard access control
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 Middleware Rate Limiter
Rate limit API requests at the edge using a sliding window counter with configurable thresholds.
Best for: API abuse prevention
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
Middleware Chain Pattern
Compose multiple middleware functions for auth, rate limiting, and geolocation in Next.js middleware.
Best for: Auth guard middleware
Middleware Geo-based Redirect
Redirect users based on their geographic location using Next.js edge middleware.
Best for: internationalization
Authentication Middleware with Session Check
Protect routes with middleware that validates session tokens and redirects unauthenticated users.
Best for: route protection
Rate Limiter for Edge Functions
Implement sliding window rate limiting in Next.js middleware using in-memory or KV store.
Best for: API protection
Multi-Tenant App with Subdomains
Build a multi-tenant application using middleware to resolve tenant from subdomain.
Best for: SaaS platforms