Skip to content

Middleware

ergo ships 21 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.

StageMiddlewarePurpose
NegotiationloggerStructured request/response logging
NegotiationacceptsContent negotiation (Accept, Accept-Language, etc.)
NegotiationcorsCross-origin resource sharing
NegotiationcookieCookie parsing
NegotiationurlURL and query string parsing
NegotiationpreferPrefer header parsing (RFC 7240)
NegotiationpreconditionConditional request enforcement
Negotiationrate-limitPer-client rate limiting
AuthorizationauthorizationBearer / Basic / API-key auth
AuthorizationcsrfCSRF token issue and verify
ValidationbodyRequest body parsing
ValidationvalidateJSON Schema validation (AJV)
Validationjson-api-queryJSON:API query validation
ValidationidempotencyIdempotency-Key enforcement
ExecutionhandlerPipeline executor
ExecutionsendResponse serialization
MiddlewarePurpose
cache-controlCache-Control header defaults
compressResponse compression (gzip, br, deflate)
security-headersSecurity response headers (CSP, HSTS, etc.)
timeoutConfigurable request deadline (setTimeout + close listener)
tracingOpenTelemetry distributed tracing

Need to write your own middleware? ergo’s 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.