46 lines
No EOL
1.4 KiB
TypeScript
46 lines
No EOL
1.4 KiB
TypeScript
import * as path from "@std/path"
|
|
export function startServer(hostname = "localhost", port = 6969) {
|
|
return Deno.serve(
|
|
{ hostname, port },
|
|
async (req) => {
|
|
const url = new URL(req.url);
|
|
const filepath = decodeURIComponent(url.pathname);
|
|
|
|
try {
|
|
const file = await Deno.open("." + filepath, { read: true });
|
|
const headers: [string, string][] = [
|
|
["Cache-Control", "no-cache"]
|
|
]
|
|
|
|
const extension = path.extname(filepath);
|
|
|
|
// chromium doesn't care about your file extensions,
|
|
// it only wants content-type
|
|
const contentType = {
|
|
'.svg': "image/svg+xml",
|
|
'.png': "image/png",
|
|
'.jpeg': "image/jpeg",
|
|
'.woff2': "font/woff2",
|
|
'.ttf': "font/ttf",
|
|
'.css': "text/css",
|
|
".html": "text/html"
|
|
}[extension]
|
|
|
|
if (contentType) {
|
|
headers.push(["Content-Type", contentType])
|
|
}
|
|
|
|
return new Response(file.readable, {
|
|
headers
|
|
});
|
|
} catch {
|
|
return new Response("404 Not Found", { status: 404 });
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
|
|
if (import.meta.main) {
|
|
startServer("localhost", 6969)
|
|
} |