Skip to content

handler

The HTTP request handler factory for ergo’s two-accumulator model. Creates domain and response accumulators per request, runs the composed pipeline, catches unexpected errors, and calls send() exactly once. This is the standalone equivalent of ergo-router’s auto-wrap.

Pipeline stage: Execution (wraps the entire pipeline)

import { handler } from "@centralping/ergo";
ArgumentTypeDescription
pipelinefunctionRequired. Composed middleware pipeline
OptionTypeDefaultDescription
debugbooleanfalseEnable pipeline debug tracing. When true, responseAcc._trace is initialized with {steps, breakAt} before the pipeline runs. On error responses (≥ 400), _trace appears as an RFC 9457 extension member.
redactErrorsbooleantrueControl whether caught 5xx exception messages appear in the RFC 9457 response detail field. When true, detail is set to generic status text. When false, err.message is passed through for 5xx only — 4xx always uses generic text. Stack traces are never exposed. Only set to false in development.
prettifybooleanfalsePretty-print JSON output (forwarded to send)
varystring[]['Accept']Vary header values to append (forwarded to send)
etagbooleantrueGenerate ETags and evaluate conditional headers (forwarded to send)
preferbooleanfalseRead domainAcc.prefer for RFC 7240 return=minimal / return=representation (forwarded to send)
paginatebooleanfalseRead domainAcc.paginate and responseAcc.paginate to auto-generate RFC 8288 Link headers and X-Total-Count for paginated responses (forwarded to send)
envelopeboolean | functionfalseWrap 2xx Object bodies in a response envelope (forwarded to send)
errorFormatterfunctionCustom error body formatter for 4xx/5xx responses. Receives the RFC 9457 Problem Details object and {requestId, statusCode, method} context. Return value becomes the response body as application/json instead of application/problem+json. (forwarded to send)

Returns an async handler (req, res) => void suitable for http.createServer().

  • Sets responseAcc.statusCode = 500 for uncaught pipeline errors (unless a timeout or earlier pipeline break already set the status)
  • Attaches instance from the X-Request-Id response header
  • Emits error on res if error listeners are present
  • If send() itself throws: res.statusCode = 500; res.end()
StatusCondition
500 Internal Server ErrorUncaught pipeline error (detail is redacted for security)
import http from "node:http";
import {
handler, compose, logger, authorization, body,
} from "@centralping/ergo";
const pipeline = compose(
{fn: logger(), setPath: "log"},
{fn: authorization({ strategies }), setPath: "auth"},
{fn: body(), setPath: "body"},
(req, res, acc) => ({
response: {
body: processRequest(acc),
statusCode: 200,
},
}),
);
const server = http.createServer(
handler(pipeline, { debug: true, prettify: true }),
);
server.listen(3000);
  • Custom Middleware — Writing middleware that fits ergo’s accumulator-based return-value contract
  • Debug Tracing — Finding which middleware rejected a request using pipeline debug mode

See the auto-generated handler API docs.