Skip to content

listen ​

app.listen(port, options?) binds the router and returns Result<HttpServer, HttpError>. The server starts accepting requests immediately, and main can continue running setup code. Use onfail to handle a bind or TLS startup error; in a void function the failure branch returns from that function.

Signatureapp.listen(port: i32, options?: ServerOptions): Result<HttpServer, HttpError>

listen returns the bound server handle on success, so server.port() reports the actual port, including when the requested port was 0. The runtime keeps the listener active after main returns and waits for an operating-system shutdown signal; call server.close() when the application needs to stop it earlier.

tyrus
import { createRouter, HttpError, HttpRouter, HttpServer } from "tyrus:http";

const main = (): void => {
    const app: HttpRouter = createRouter();
    const server = app.listen(3000) onfail {
        printError(`${server.code}: ${server.message}`);
    };
    print(`Server listening on port ${server.port()}`);
};

Handle startup failure ​

The onfail binding contains HttpError, with a stable code and a readable message. It handles the error without throwing; the branch returns from this void function after reporting it.

tyrus
import { createRouter, HttpError, HttpRouter, HttpServer } from "tyrus:http";

const main = (): void => {
    const app: HttpRouter = createRouter();
    const server = app.listen(3000) onfail {
        printError(`${server.code}: ${server.message}`);
    };
    print(`Server listening on port ${server.port()}`);
};

Options ​

Pass a ServerOptions object as the second argument to listen. See ServerOptions for the available settings.