import type { IncomingMessage, ServerResponse } from "node:http"; export function json(res: ServerResponse, data: unknown, status = 200): void { res.writeHead(status, { "Content-Type": "application/json" }); res.end(JSON.stringify(data)); } export function readBody(req: IncomingMessage): Promise> { return new Promise((resolve, reject) => { let body = ""; req.on("data", (chunk: Buffer) => { body += chunk.toString(); }); req.on("end", () => { try { resolve(JSON.parse(body)); } catch { reject(new Error("无效 JSON")); } }); req.on("error", reject); }); }