Overview
The HTTP trigger supports Hono middleware for adding cross-cutting concerns like logging, compression, security headers, and custom logic to your API endpoints. Middleware can be used in:- YAML ensembles - Reference by string name
- TypeScript ensembles - Pass functions directly
Built-in Middleware
Conductor ships with 6 Hono middleware handlers ready to use:| Name | Package | Description |
|---|---|---|
logger | hono/logger | Logs HTTP requests/responses to console |
compress | hono/compress | Gzip/Brotli response compression |
timing | hono/timing | Adds Server-Timing header for performance metrics |
secure-headers | hono/secure-headers | Security headers (X-Frame-Options, CSP, etc) |
pretty-json | hono/pretty-json | Pretty-prints JSON responses (dev only) |
etag | hono/etag | Generates ETags for cache validation |
Quick Example
Middleware Execution Order
Middleware executes in this specific order:- CORS (if configured in trigger)
- Rate Limiting (if configured in trigger)
- Cache (if configured in trigger)
- Custom Middleware (your
middlewarearray) - Auth (if configured in trigger)
- Main Handler (ensemble execution)
Custom Middleware
Register your own middleware in your worker’s initialization.Basic Registration
Using Custom Middleware
Advanced Custom Middleware
Common Patterns
Production API
Full production setup with security and performance:Development API
Focused on visibility and debugging:Public Content Site
Optimized for caching and performance:Mixed TypeScript + YAML
You can mix both approaches in the same project:Project Organization
Option 1: Simple Projects
Register middleware in your worker entry point:Option 2: Organized Projects
Create dedicated middleware directory:Option 3: Reusable Plugins
Package middleware in Conductor plugins:Debugging Middleware
List all registered middleware:API Reference
HttpMiddlewareRegistry
getHttpMiddlewareRegistry()
Get the global singleton registry instance:
Best Practices
- Order Matters - Place security middleware (
secure-headers) early, compression (compress) late - YAML for Production - Use named middleware in YAML for better visibility and consistency
- TypeScript for Prototyping - Use direct functions for one-off custom logic
- Register Once - Register custom middleware during app initialization, not per-request
- Performance - Only use middleware you need - each adds latency
- Testing - Test middleware in isolation before adding to production ensembles

