Implementation:Microsoft Playwright HttpServer
Appearance
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
wsServerlibrary - 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
routePrefixorroutePath - Start options: port, hostname
Outputs
- HTTP server listening on the specified port
- URL prefix string for constructing full URLs
- Route handlers return
trueif they handled the request
Related Pages
- Microsoft_Playwright_WsServer -- WebSocket server implementation
- Microsoft_Playwright_NetworkUtils -- HTTP request and server creation utilities
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment