Libraries v5.2.0
    Preparing search index...

    Class ServerBuilder

    Fluent builder for constructing and starting an HTTP server.

    const server = new ServerBuilder();

    server
    .services(svc => svc.addSingleton(IDb, () => new Db()))
    .use(loggingMiddleware)
    .handle(GetUser, ({ params }) => db.find(params.id));

    await server.listen(3000);
    Index
    • Register an additional content type handler for content negotiation. JSON is registered by default.

      Parameters

      Returns this

    • Returns the authentication configuration, or null if useAuthentication() has not been called.

      Returns AuthenticationConfig | null

    • Returns a snapshot of all registered endpoints. Useful for generating OpenAPI specs or other documentation.

      Returns readonly EndpointRegistration[]

    • Returns a snapshot of all registered WebSocket subscription endpoints. Consumed by @cleverbrush/server-openapi to emit the AsyncAPI spec.

      Returns readonly SubscriptionRegistration[]

    • Returns a snapshot of all registered webhook definitions. Consumed by @cleverbrush/server-openapi to emit the webhooks map.

      Returns readonly WebhookDefinition[]

    • Register an endpoint and its handler.

      Type Parameters

      • E extends EndpointBuilder<any, any, any, any, any, any, any, any, any, false>

      Parameters

      • endpointDef: E

        An EndpointBuilder instance (e.g. from endpoint.get(...)).

      • handler: Handler<E>

        The typed handler function.

      • Optionaloptions: EndpointOptions<E>

        Optional per-endpoint middleware.

      Returns this

    • Register all endpoints from a HandlerMapping created by mapHandlers. This is the bulk equivalent of calling .handle() for each endpoint individually.

      Parameters

      • mapping: HandlerMapping

        The mapping produced by mapHandlers(endpoints, handlers).

      Returns this

    • Start listening on the given port and host. Resolves with the running Server instance.

      Parameters

      • Optionalport: number

        TCP port (default: ServerOptions.port ?? 3000).

      • Optionalhost: string

        Bind address (default: ServerOptions.host ?? '0.0.0.0').

      Returns Promise<Server>

    • Configure the DI service collection.

      Parameters

      • configureFn: (svc: ServiceCollection) => void

        Receives the ServiceCollection for registrations.

      Returns this

    • Add middleware to the matched-request pipeline after authentication. Middleware is executed in the order it is added. CORS short-circuits and unmatched routes do not enter this pipeline.

      Parameters

      Returns this

    • Enable authentication with one or more schemes. Registers middleware that authenticates protected requests and sets ctx.principal. CORS preflights are handled before this middleware.

      Parameters

      Returns this

    • Enable authorization enforcement. Registers a global middleware that checks endpoint authorize() metadata against the authenticated principal. Must be called after useAuthentication().

      Parameters

      Returns this

    • Enable the server-side request batching endpoint.

      Once enabled, the server accepts POST <path> (default /__batch) containing an array of sub-requests and processes each one through the full middleware and handler pipeline, returning an array of sub-responses in a single HTTP reply.

      Pair this with the batching() middleware from @cleverbrush/client/batching on the client side.

      Parameters

      Returns this

      new ServerBuilder()
      .useBatching()
      .handleAll(mapping)
      .listen(3000);
    • Enable server-wide CORS before routing and authentication. Accepted preflights return 204 without running ordinary middleware. Requests with disallowed origins return 403 before handlers run. Configuration is validated and copied when the server starts listening.

      Parameters

      Returns this

    • Wrap every HTTP request before CORS, routing and authentication. Use for tracing and completion observers; route, services and principal are populated later. Ordinary middleware retains its existing order.

      Parameters

      Returns this

    • Register a webhook definition.

      Webhooks are recorded for OpenAPI spec generation only — they are not served as HTTP routes by the runtime server.

      Parameters

      Returns this

    • Enable the GET /health endpoint that returns { ok: true } (200). Useful for load balancer and container readiness probes.

      The path is available as HEALTHCHECK_PATH.

      Returns this