import http from "http"; import fs from "fs"; import path from "path"; import url from "url"; const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); const ROOT = path.resolve(__dirname, ".."); const PORT = process.env.PORT || 5173; const MIME = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".ttf": "font/ttf", ".woff": "font/woff", ".woff2": "font/woff2", }; const server = http.createServer((req, res) => { let reqPath = req.url.split("?")[0]; if (reqPath === "/") { reqPath = "/index.html"; } const filePath = path.join(ROOT, reqPath); fs.stat(filePath, (err, stat) => { if (err || !stat.isFile()) { res.writeHead(404, { "content-type": "text/plain; charset=utf-8" }); res.end("404 Not Found"); return; } const ext = path.extname(filePath).toLowerCase(); const ctype = MIME[ext] || "application/octet-stream"; res.writeHead(200, { "content-type": ctype, "cache-control": "no-cache" }); fs.createReadStream(filePath).pipe(res); }); }); server.listen(PORT, () => { console.log(`[serve] http://localhost:${PORT}`); });