typescriptintermediate
Request ID Tracing Middleware
Express middleware that generates or forwards X-Request-Id headers for distributed tracing.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { Request, Response, NextFunction } from 'express';
import { randomUUID } from 'crypto';
export function requestIdMiddleware(req: Request, res: Response, next: NextFunction) {
const requestId = (req.headers['x-request-id'] as string) || randomUUID();
req.headers['x-request-id'] = requestId;
res.setHeader('X-Request-Id', requestId);
next();
}Use Cases
- Distributed tracing
- Log correlation across microservices
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
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
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