Skip to content

cors

Validates CORS requests against allowed origins, methods, and headers. No-op when the Origin header is absent (same-origin requests). Preflight OPTIONS requests are handled at the router level by ergo-router.

Pipeline stage: Negotiation

import { cors } from "@centralping/ergo";
OptionTypeDefaultDescription
originsstring | string[] | RegExp | function'*'Allowed origins
allowMethodsstring[]All standard methodsAllowed HTTP methods (default: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE)
allowHeadersstring | string[] | RegExp | function'*'Allowed request headers
exposeHeadersstring | string[]Headers exposed to the client
allowCredentialsbooleanfalseAllow credentials (cookies, auth headers)
maxAgenumberPreflight cache duration in seconds

The middleware returns one of three results:

  • No Origin header: undefined (no-op, same-origin request)
  • CORS allowed: { response: { headers: [...] } } with Access-Control-Allow-Origin and related headers
  • CORS denied: { response: { statusCode: 403 } }
StatusCondition
403 ForbiddenOrigin header present but not allowed by policy
import { compose, cors } from "@centralping/ergo";
const pipeline = compose(
[cors({
origins: ["https://app.example.com"],
allowMethods: ["GET", "POST"],
allowHeaders: ["Authorization", "Content-Type"],
}), "cors"],
);

See the auto-generated cors API docs.