Skip to content

Middleware

ergo ships 22 middleware factories covering every stage of the Fast Fail pipeline. Each guide page documents the full options table, return value, error responses, and usage examples for both standalone composition and declarative ergo-router configuration.

Stage Middleware Purpose
Negotiation logger Structured request/response logging
Negotiation accepts Content negotiation (Accept, Accept-Language, etc.)
Negotiation cors Cross-origin resource sharing
Negotiation cookie Cookie parsing
Negotiation url URL and query string parsing
Negotiation paginate Pagination parameter parsing (offset + cursor)
Negotiation prefer Prefer header parsing (RFC 7240)
Negotiation precondition Conditional request enforcement
Negotiation rate-limit Per-client rate limiting
Authorization authorization Bearer / Basic / API-key auth
Authorization csrf CSRF token issue and verify
Validation body Request body parsing
Validation validate JSON Schema validation (AJV)
Validation json-api-query JSON:API query validation
Validation idempotency Idempotency-Key enforcement
Execution handler Pipeline executor
Execution send Response serialization
Middleware Purpose
cache-control Cache-Control header defaults
compress Response compression (gzip, br, deflate)
security-headers Security response headers (CSP, HSTS, etc.)
timeout Configurable request deadline (setTimeout + close listener)
tracing OpenTelemetry distributed tracing

Need to write your own middleware? ergo’s domain accumulator-based return-value contract lets you build middleware that composes cleanly with the pipeline — feature flags, audit logging, request enrichment, or third-party integrations.

See the Custom Middleware recipe for the full contract, working examples, and anti-patterns to avoid.

Each middleware has two documentation surfaces:

  • Guide pages (this section) — options, examples, error tables, and RFC references written for developers building with ergo.
  • API Reference — auto-generated from JSDoc with precise type signatures. Linked at the bottom of each guide page.