import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; import postBlob from "../blob/index.ts"; import getBlob from "../blob/[id].ts"; import { sendJson } from "./httpUtils.ts"; export function createApiServer() { return createServer(async (req: IncomingMessage, res: ServerResponse) => { const pathname = new URL(req.url ?? "/", "http://localhost").pathname; try { if (pathname === "/api/blob") { await postBlob(req, res); return; } if (pathname.startsWith("/api/blob/")) { await getBlob(req, res); return; } sendJson(res, 404, { error: "not found" }); } catch (err) { sendJson(res, 500, { error: "internal error", detail: (err as Error).message }); } }); } if (import.meta.url === `file://${process.argv[1]}`) { const port = Number(process.env.PORT ?? 3001); createApiServer().listen(port, () => { console.log(`blob API dev server listening on http://localhost:${port}`); }); }