summaryrefslogtreecommitdiff
path: root/std/http/file_server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/http/file_server.ts')
-rwxr-xr-xstd/http/file_server.ts22
1 files changed, 20 insertions, 2 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts
index 4ddec7000..79f3e5081 100755
--- a/std/http/file_server.ts
+++ b/std/http/file_server.ts
@@ -7,7 +7,6 @@
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
const { args, stat, readdir, open, exit } = Deno;
-import { contentType } from "../media_types/mod.ts";
import { posix, extname } from "../path/mod.ts";
import { listenAndServe, ServerRequest, Response } from "./server.ts";
import { parse } from "../flags/mod.ts";
@@ -41,6 +40,25 @@ const CORSEnabled = serverArgs.cors ? true : false;
const target = posix.resolve(serverArgs._[1] ?? "");
const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4500}`;
+const MEDIA_TYPES: Record<string, string> = {
+ ".md": "text/markdown",
+ ".html": "text/html",
+ ".htm": "text/html",
+ ".json": "application/json",
+ ".map": "application/json",
+ ".txt": "text/plain",
+ ".ts": "application/typescript",
+ ".tsx": "application/typescript",
+ ".js": "application/javascript",
+ ".jsx": "application/jsx",
+ ".gz": "application/gzip",
+};
+
+/** Returns the content-type based on the extension of a path. */
+function contentType(path: string): string | undefined {
+ return MEDIA_TYPES[extname(path)];
+}
+
if (serverArgs.h ?? serverArgs.help) {
console.log(`Deno File Server
Serves a local directory in HTTP.
@@ -104,7 +122,7 @@ export async function serveFile(
const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]);
const headers = new Headers();
headers.set("content-length", fileInfo.size.toString());
- const contentTypeValue = contentType(extname(filePath));
+ const contentTypeValue = contentType(filePath);
if (contentTypeValue) {
headers.set("content-type", contentTypeValue);
}