Files
yunzerwebsiteallinone/milktea-site/preview-server.mjs
T
2026-10-19 01:13:17 +08:00

25 lines
856 B
JavaScript

import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join } from "node:path";
const root = join(import.meta.dirname, "dist");
const port = Number(process.argv[2] || 4173);
const types = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".svg": "image/svg+xml",
};
createServer(async (req, res) => {
const path = new URL(req.url, "http://127.0.0.1").pathname;
const file = path === "/" ? "index.html" : path;
try {
const body = await readFile(join(root, file));
res.writeHead(200, { "content-type": types[extname(file)] || "application/octet-stream" });
res.end(body);
} catch {
res.writeHead(404).end("not found");
}
}).listen(port, "127.0.0.1", () => console.log(`preview http://127.0.0.1:${port}/`));