// src/server.mjs import { createServer } from 'node:http'; // A request's abort signal. The deployed runtime attaches its own signal to each // request, and it aborts at the request's window. A local run has none of its own, // and Node's request.signal can abort at a normal request end on some Node 24 // releases, so a local run's signal aborts when the response closes unfinished. function abortSignalOf(request, response) { const own = Object.hasOwn(request, 'signal') ? request.signal : undefined; if (own) return own; const controller = new AbortController(); response.once('close', () => { if (!response.writableFinished) controller.abort(); }); return controller.signal; } export function createAppServer() { return createServer((request, response) => { // End the response when the request is aborted (abortSignalOf, above). const signal = abortSignalOf(request, response); signal.addEventListener('abort', () => response.destroy(), { once: true }); const send = (status, type, body) => { if (response.destroyed) return; response.writeHead(status, { 'content-type': type }); response.end(body); }; // A target that does not parse as a URL names no route, so it is answered 404 below. let pathname = null; try { pathname = new URL(request.url, 'http://localhost').pathname; } catch {} if (request.method === 'GET' && pathname === '/health') return send(200, 'application/json', JSON.stringify({ ok: true })); if (request.method === 'GET' && pathname === '/api/hello') return send(200, 'text/plain; charset=utf-8', 'hello world'); send(404, 'application/json', JSON.stringify({ error: 'not_found' })); }); }