Express.js Integration
The @vestig/express package provides seamless integration with Express.js, offering middleware for automatic request logging, correlation ID propagation, and error handling.
Installation
bash
Quick Start
typescript
Middleware
vestigMiddleware
The pre-configured middleware for immediate use:
typescript
This middleware automatically:
- Generates correlation IDs for each request (or extracts from headers)
- Logs incoming requests with method, path, and headers
- Logs responses with status code and duration
- Propagates W3C Trace Context via
traceparentheaders - Sets up AsyncLocalStorage context for downstream handlers
Custom Middleware Configuration
Use createVestigMiddleware for custom configuration:
typescript
Middleware Options
| Option | Type | Default | Description |
|---|---|---|---|
level |
LogLevel |
'info' |
Log level for middleware logger |
enabled |
boolean |
true |
Enable/disable logging |
sanitize |
SanitizePreset |
'default' |
PII sanitization preset |
namespace |
string |
'http' |
Logger namespace |
skipPaths |
string[] |
[] |
Path prefixes to skip |
requestIdHeader |
string |
'X-Request-ID' |
Header for request ID |
timing |
boolean |
true |
Enable timing logs |
requestLogLevel |
LogLevel |
'info' |
Level for request logs |
responseLogLevel |
LogLevel |
'info' |
Level for response logs |
structured |
boolean |
true |
Use JSON output |
Route Handlers
withVestig Wrapper
Wrap route handlers to get typed access to logging context:
typescript
Route Handler Context
The context object provides:
typescript
Route Handler Options
typescript
Route Handler Factory
Create a factory with preset options:
typescript
Error Handling
vestigErrorHandler
The pre-configured error handler:
typescript
Error responses include correlation IDs for debugging:
json
Custom Error Handler
typescript
404 Not Found Handler
typescript
Custom not found message:
typescript
Correlation Context
Automatic Propagation
The middleware automatically handles correlation ID propagation:
typescript
Accessing Context in Handlers
typescript
Calling Downstream Services
typescript
Request Timing
Timing Utilities
typescript
Complete Example
typescript
API Reference
Exports
| Export | Type | Description |
|---|---|---|
vestigMiddleware |
RequestHandler |
Pre-configured middleware |
createVestigMiddleware |
Function |
Middleware factory |
withVestig |
Function |
Route handler wrapper |
createRouteHandler |
Function |
Route handler factory |
vestigErrorHandler |
ErrorRequestHandler |
Pre-configured error handler |
createVestigErrorHandler |
Function |
Error handler factory |
notFoundHandler |
RequestHandler |
Pre-configured 404 handler |
createNotFoundHandler |
Function |
404 handler factory |
Types
typescript
Best Practices
- Order matters: Place middleware before routes, error handler last
- Skip health checks: Use
skipPathsfor high-frequency endpoints - Use factories: Create route handler factories for consistent configuration
- Propagate context: Forward correlation headers to downstream services
- Handle errors properly: Use the error handler to capture all errors with context