-
Notifications
You must be signed in to change notification settings - Fork 1
/
prod.ts
37 lines (31 loc) · 962 Bytes
/
prod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
const currentBuildPath = path.join(await Deno.cwd(), "build/");
await serve(
async (request: Request) => {
const url = new URL(request.url);
const filepath = url.pathname ? decodeURIComponent(url.pathname) : "";
let file;
if (filepath !== "/") {
try {
file = await Deno.open(currentBuildPath + filepath, { read: true });
} catch {}
}
if (!file) {
let indexFileText = await Deno.readTextFile(
currentBuildPath + "index.html",
);
return new Response(indexFileText, {
headers: {
"content-type": "text/html",
},
});
}
return new Response(file?.readable, {
headers: {
"content-type": filepath.includes(".css") ? "text/css" : null,
},
});
},
{ port: Deno.env.get("PORT") || 8080 },
);