-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
38 lines (31 loc) · 1.12 KB
/
server.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
38
import { serve } from "https://deno.land/std@0.176.0/http/server.ts";
import { join, extname } from "https://deno.land/std@0.176.0/path/mod.ts";
// Ensure the static assets are in the root directory
const ROOT_DIR = "./"; // The root directory where static files are located
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname === "/" ? "/index.html" : url.pathname;
const filePath = join(ROOT_DIR, path);
try {
const file = await Deno.readFile(filePath);
const ext = extname(filePath).toLowerCase();
const mimeTypes: { [key: string]: string } = {
".html": "text/html",
".css": "text/css",
".js": "application/javascript",
".ttf": "font/ttf",
};
const contentType = mimeTypes[ext] || "application/octet-stream";
return new Response(file, {
status: 200,
headers: {
"Content-Type": contentType,
},
});
} catch (e) {
return new Response("Not Found", { status: 404 });
}
}
// Start server on port 8000
serve(handler, { port: 8000 });
console.log("Server is running at port 8000");