hammerspoon2-docs
    Preparing search index...

    Class HSHTTPServer

    An HTTP server instance created by hs.httpserver.create(). Configure with chainable setter methods, then call start() to begin accepting connections. The server supports synchronous and async (Promise-returning) request callbacks, optional static file serving, HTTP Basic authentication, Bonjour advertisement, and TLS via PKCS#12. Do not instantiate HSHTTPServer directly — use hs.httpserver.create().

    Index

    Constructors

    Properties

    identifier: string

    A unique identifier for this server instance (UUID string).

    Methods

    • Destroy this server, releasing all resources. After calling destroy(), the server object should not be used.

      Returns void

    • Get the configured network interface, or null if listening on all interfaces.

      Returns string | null

      The interface name or IP address string, or null.

    • Get the configured Bonjour service name.

      Returns string

      The Bonjour service name.

    • Get the TCP port the server is currently listening on. Returns 0 if the server is not running.

      Returns number

      The TCP port number.

    • Enable or disable directory listing for requests that map to a directory with no index file. When disabled (the default), directory requests without an index file return 403.

      Parameters

      • allow: boolean

        true to serve directory listings, false to return 403 (default).

      Returns HSHTTPServer

      This server, for chaining.

    • Enable or disable Bonjour advertisement of this server on the local network.

      Parameters

      • enable: boolean

        true to advertise via Bonjour, false to disable (default).

      Returns HSHTTPServer

      This server, for chaining.

    • Set the request handler callback. If the callback returns null or undefined, the server falls through to static file serving (if a document root is set), or responds with 404.

      Parameters

      • callback:
            | (
                (
                    method: string,
                    path: string,
                    headers: object,
                    body: string,
                ) =>
                    | { body: string; headers: object; status: number }
                    | Promise<{ body: string; headers: object; status: number }>
            )
            | null

        The request handler, or null to clear.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the list of index filenames checked when a directory is requested. Defaults to ["index.html", "index.htm"]. Files are checked in order.

      Parameters

      • files: string[]

        Array of filename strings.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the filesystem path to serve static files from. When a document root is set, requests not handled by the callback are served as static files from this directory. Pass null to disable static file serving.

      Parameters

      • path: string | null

        Absolute path to a directory, or null to disable.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the network interface to listen on. Pass null to listen on all interfaces (the default). Pass "localhost" or "loopback" to restrict to the loopback interface only.

      Parameters

      • iface: string | null

        Interface name or IP address string, or null for all interfaces.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the maximum allowed incoming request body size in bytes. Requests with a body exceeding this limit receive a 413 response. Defaults to 10 MB.

      Parameters

      • size: number

        Maximum body size in bytes.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the Bonjour service name advertised on the local network. Only used when Bonjour is enabled via setBonjour(true).

      Parameters

      • name: string

        The Bonjour service name.

      Returns HSHTTPServer

      This server, for chaining.

    • Set a password required for Basic authentication. When set, every request must supply an Authorization: Basic header with any username and the configured password. Pass null to disable authentication.

      Parameters

      • password: string | null

        The required password, or null to remove authentication.

      Returns HSHTTPServer

      This server, for chaining.

    • Set the TCP port to listen on. Must be called before start(). Pass 0 to let the OS assign an available port (use getPort() after start() to discover it).

      Parameters

      • port: number

        TCP port number (0–65535).

      Returns HSHTTPServer

      This server, for chaining.

    • Configure TLS using a PKCS#12 (.p12) identity file. When TLS is configured, the server accepts HTTPS connections. The .p12 file must contain both the certificate and the private key.

      Parameters

      • path: string

        Absolute path to the .p12 file.

      • password: string

        The password protecting the .p12 file.

      Returns HSHTTPServer

      This server, for chaining.

    • Register a WebSocket handler for a URL path. When a client connects and performs a WebSocket upgrade handshake on path, the callback is invoked with three arguments: event (string), connection (HSWebSocketConnection), and message (string). Events:* Pass null to remove the WebSocket handler for the path.

      Parameters

      • path: string

        The URL path to handle WebSocket connections on (e.g. "/ws").

      • callback:
            | (
                (
                    event: string,
                    connection: HSWebSocketConnection,
                    message: string,
                ) => void
            )
            | null

        The event handler, or null to remove.

      Returns HSHTTPServer

      This server, for chaining.

    • Start the server and begin accepting connections. The server must be configured before calling start(). To restart the server with new settings, call stop() followed by start().

      Returns HSHTTPServer

      This server, for chaining.