23 lines
871 B
JavaScript
23 lines
871 B
JavaScript
const root = `${import.meta.dir}/../dist`;
|
|
const contentTypes = {
|
|
".html": "text/html; charset=utf-8",
|
|
".css": "text/css; charset=utf-8",
|
|
".js": "text/javascript; charset=utf-8",
|
|
".svg": "image/svg+xml"
|
|
};
|
|
|
|
Bun.serve({
|
|
port: Number(process.env.PORT || 4173),
|
|
async fetch(request) {
|
|
const url = new URL(request.url);
|
|
const relative = url.pathname === "/" ? "/index.html" : url.pathname;
|
|
if (relative.includes("..")) return new Response("Not found", { status: 404 });
|
|
const file = Bun.file(`${root}${relative}`);
|
|
if (!(await file.exists())) return new Response("Not found", { status: 404 });
|
|
const extension = relative.slice(relative.lastIndexOf("."));
|
|
return new Response(file, { headers: { "content-type": contentTypes[extension] || "application/octet-stream" } });
|
|
}
|
|
});
|
|
|
|
console.log("如饮AI preview: http://127.0.0.1:4173");
|