BrowserRouter - HTTP routing and middleware management for KOS web services.

The BrowserRouter model provides a comprehensive HTTP routing system that handles incoming requests via WebSocket topics and routes them through registered middleware and handlers. It enables KOS applications to expose REST APIs, handle device communication, and manage HTTP-based integrations with automatic OpenAPI specification generation.

Key Features

  • Topic-Based HTTP Handling - Processes HTTP requests via kos.http.request topic
  • Middleware Support - Chain multiple middleware functions for request processing
  • Route Management - Dynamic registration, removal, and inspection of routes
  • OpenAPI Generation - Automatic API documentation at /openapi.json
  • Request/Response Abstraction - Familiar Express.js-style request/response objects
  • Method Support - Full HTTP method support (GET, POST, PUT, DELETE, PATCH)

Topic-Based Reactivity

The model listens to these KOS topics for HTTP request handling:

  • kos.http.request - Incoming HTTP requests from clients or other services
  • kos.http.response - Outgoing HTTP responses with status codes and data

Common Use Cases

  • REST API Endpoints - Expose device data and control APIs
  • Webhook Handlers - Process incoming webhooks from external services
  • Inter-Service Communication - Handle requests between KOS services
  • Device Integration - Bridge HTTP protocols with KOS messaging
  • API Gateway - Route and transform requests to downstream services

Example: Basic Usage

const router = KosBrowserRouter.instance('main-router')
.options({})
.build();

// Register HTTP endpoints with middleware support
router.use('GET', '/api/status', async (req, res) => {
res.send({ status: 'online' });
});

router.use('POST', '/api/commands', validateAuth, async (req, res) => {
res.status(200).send({ success: true });
});

Use Declared Type

See

BrowserRouterOptions - Configuration options for BrowserRouter instances

interface KosBrowserRouter {
    use<TPath>(method, path, ...middlewares): void;
    remove(method, path): number;
    removeAllForPath(path): number;
    getRoutes(): {
        method: string;
        path: string;
    }[];
}

Methods

  • Registers a route with optional middleware chain.

    Type Parameters

    • TPath extends string = string

    Parameters

    • method: KosMethodTypes

      HTTP method (GET, POST, PUT, DELETE, PATCH)

    • path: TPath

      Route path pattern with parameter support

    • Rest ...middlewares: KosMiddleware<TPath>[]

      Middleware functions and final handler

    Returns void

  • Remove routes matching the given method and path

    Parameters

    • method: KosMethodTypes

      HTTP method (GET, POST, etc.)

    • path: string

      Route path pattern

    Returns number

    Number of routes removed

  • Remove all routes matching a path pattern (regardless of method)

    Parameters

    • path: string

      Route path pattern

    Returns number

    Number of routes removed

  • Gets all registered routes for debugging and monitoring.

    Returns {
        method: string;
        path: string;
    }[]

    Array of route objects with method and path