Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright HttpServer

From Leeroopedia

Template:Implementation Page

Overview

HttpServer provides a configurable HTTP server with route handling, static file serving, and optional WebSocket upgrade support, used for Playwright's internal servers like the trace viewer.

Description

The HttpServer class wraps a Node.js HTTP server with:

  • Route matching -- supports both prefix-based and exact-path route handlers
  • Static file serving -- serves files from directories with proper MIME type detection
  • WebSocket support -- optional WebSocket upgrade handling using the wsServer library
  • URL management -- generates both precise and human-readable URL prefixes
  • GUID-based security -- optional WebSocket paths include a GUID for access control

Usage

Used internally for the trace viewer server, HTML reporter server, and other built-in HTTP-based tools.

Code Reference

Source Location

packages/playwright-core/src/server/utils/httpServer.ts (228 lines)

Class Signature

export type ServerRouteHandler = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;

export class HttpServer {
  constructor()
  server(): http.Server
  routePrefix(prefix: string, handler: ServerRouteHandler): void
  routePath(path: string, handler: ServerRouteHandler): void
  serveFile(request: http.IncomingMessage, response: http.ServerResponse, absoluteFilePath: string, headers?: Record<string, string>): boolean
  async start(options?: { port?: number; host?: string; preferredPort?: number }): Promise<string>
  async stop(): Promise<void>
  urlPrefix(purpose?: 'human-readable'): string
}

Import

import { HttpServer } from './server/utils/httpServer';
import type { ServerRouteHandler } from './server/utils/httpServer';

I/O Contract

Inputs

  • Route handlers registered via routePrefix or routePath
  • Start options: port, hostname

Outputs

  • HTTP server listening on the specified port
  • URL prefix string for constructing full URLs
  • Route handlers return true if they handled the request

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment