Minimal HTTP + WebSocket server used to serve test bundles and push reload events.
Constructing one binds nothing — routes and middleware are plain registrations until
listen(), so setup is cheap and safe to unwind:
const server = new HTTPServer(); server.get('/health', (_req, res) => res.end('ok')); await server.close(); // never listened; teardown is still safe
serve(config?: { port: number; onListen?: (s: object) => void; onError?: (e: Error) => void; },handler: (req: http.IncomingMessage,res: http.ServerResponse) => void): Promise<http.Server>
Creates and starts a plain http.createServer instance on the given port.
The handler gets node's own request and response, NOT this server's Request and
Response: nothing in this path attaches path/query/params/json, so promising
them would be a lie. The ambient augmentation this replaced told exactly that lie.
// Defined, not invoked: binds a real port. function example() { return HTTPServer.serve({ port: 4200 }, (_req, res) => res.end('static-style handler')); }
close(): Promise<void>
Closes the underlying HTTP server and all active connections, returning a Promise that resolves once the server is fully closed.
const server = new HTTPServer(); await server.close(); // resolves even if listen() never happened
delete(path: string,handler: RouteHandler): void
Registers a DELETE route handler.
const server = new HTTPServer(); server.delete('/runs/:id', (_req, res) => res.end()); await server.close();
get(path: string,handler: RouteHandler): void
Registers a GET route handler.
const server = new HTTPServer(); server.get('/tests/:id', (req, res) => res.end(req.params.id)); // :id lands in req.params await server.close();
listen(port?: number,callback?: () => void): Promise<void>
Starts listening on the given port (0 = OS-assigned).
// Defined, not invoked: binds a real port. async function example(server: HTTPServer) { await server.listen(0); // 0 → the OS assigns; read it from server._server.address() }
post(path: string,handler: RouteHandler): void
Registers a POST route handler.
const server = new HTTPServer(); server.post('/report', (_req, res) => res.json({ accepted: true })); await server.close();
publish(data: string): void
Broadcasts a message to all connected WebSocket clients.
const server = new HTTPServer(); server.publish(JSON.stringify({ event: 'refresh' })); // zero clients connected — a no-op await server.close();
put(path: string,handler: RouteHandler): void
Registers a PUT route handler.
const server = new HTTPServer(); server.put('/runs/:id', (_req, res) => res.end()); await server.close();
use(middleware: Middleware): void
Adds a middleware function to the chain.
const server = new HTTPServer(); server.use((_req, _res, next) => next()); // runs before every route handler await server.close();