file server content type headers

This commit is contained in:
2025-01-20 23:45:07 -07:00
parent 3b1a969145
commit fcba2014c2
4 changed files with 36 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import type {
RouteConfigurator,
RouterContext,
} from "./types.ts";
import { getContentTypeByExtension } from "./util/contentType.ts";
import { NotFound } from "./util/response.ts";
/**
@@ -392,7 +393,9 @@ export class Router {
try {
const file = await Deno.readFile(normalizedPath);
return new Response(file);
const filetype = normalizedPath.split(".")[1];
const contentType = getContentTypeByExtension(filetype);
return new Response(file, { headers: { "Content-Type": contentType } });
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return showIndex ? generateIndex(normalizedPath) : NotFound();