import { createReadStream, existsSync } from "node:fs"; import { extname, join, normalize } from "node:path"; import { createServer } from "node:http"; const port = Number(process.env.PORT || 5173); const root = process.cwd(); const mimeTypes = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", }; function resolveRequest(url) { const pathname = decodeURIComponent(new URL(url, `http://localhost:${port}`).pathname); const requestedPath = normalize(join(root, pathname === "/" ? "index.html" : pathname)); if (!requestedPath.startsWith(root)) { return null; } return requestedPath; } createServer((request, response) => { const filePath = resolveRequest(request.url || "/"); if (!filePath || !existsSync(filePath)) { response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); response.end("Not found"); return; } response.writeHead(200, { "Content-Type": mimeTypes[extname(filePath)] || "application/octet-stream", }); createReadStream(filePath).pipe(response); }).listen(port, () => { console.log(`VitePress-CMS prototype running at http://localhost:${port}`); });