Libraries v5.0.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 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

    • 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>

    • 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 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

    • 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