42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { contentType } from "@std/media-types";
|
|
import path from "node:path";
|
|
|
|
const NET_PORT = 4536;
|
|
const NET_HOST = '10.0.1.39';
|
|
const WEB_ROOT = './res/';
|
|
|
|
Deno.serve({
|
|
hostname: NET_HOST,
|
|
port: NET_PORT,
|
|
onListen: addr => console.log(`Listening on http://${addr.hostname}:${addr.port}`)
|
|
}, req => {
|
|
const url = new URL(req.url);
|
|
|
|
if (url.pathname == '/') url.pathname = '/index.html';
|
|
|
|
return new Promise<Response>(resolve => {
|
|
Deno.readFile(path.join(WEB_ROOT, url.pathname)).then(data => {
|
|
|
|
const pathParts = url.pathname.split('/');
|
|
const lastPart = pathParts[pathParts.length - 1];
|
|
const extensionParts = lastPart.split('.');
|
|
const extension = extensionParts[extensionParts.length - 1];
|
|
|
|
const mime = contentType(extension);
|
|
if (!mime) throw new Error("No mime-type found");
|
|
|
|
console.log(`${req.method} ${url}`);
|
|
resolve(new Response(data, { headers: { "Content-Type": mime }}))
|
|
|
|
}).catch(() => {
|
|
|
|
console.log(`404 ${req.method} ${url}`);
|
|
resolve(new Response("Not Found. Did you try thinking Miku?", {
|
|
status: 404,
|
|
statusText: "Not Found (oo-ee-oo)",
|
|
headers: { "Content-Type": "text/plain" }
|
|
}));
|
|
|
|
});
|
|
})
|
|
}); |