About Middleware
Reusable Express middleware
Browse the snippets below to find the middleware implementation you need for your Express.js project.
All Middleware Snippets
JWT Authentication
JWT middleware with role-based access
- JWT verification
- Role-based access control
- Token extraction from header/cookie
API Key Authentication
API key auth for service-to-service
- Header-based API key
- Multiple key support
- Key validation
Winston Logger
Structured logging with Winston
- Multiple log levels
- JSON formatting
- File and console transports
Routes Logger
HTTP request logging middleware
- Morgan integration
- Customizable format
- Skip conditions
Error Handler
Global error handling middleware
- Global error catching
- Environment-aware responses
- Error logging
Zod Validation
Request validation with Zod schemas
- Body validation
- Query and params validation
- Type inference
Async Handler
Async route wrapper for error handling
- Automatic error catching
- Promise rejection handling
- Clean route definitions
Cache Control
HTTP caching headers middleware
- Cache-Control headers
- ETag support
- Conditional requests
Compression
Response compression middleware
- Gzip compression
- Brotli support
- Threshold configuration
Request ID
Unique ID for request tracing
- UUID generation
- Header propagation
- Logging integration
Response Time
Add X-Response-Time header
- Millisecond precision
- Header injection
- Callback support
How to Use
Install any snippet using the Hanma CLI:
npx hanma add --framework express
For example, to install the first snippet:
npx hanma add jwt-auth