summaryrefslogtreecommitdiff
path: root/file_server.ts
diff options
context:
space:
mode:
author木杉 <zhmushan@qq.com>2018-12-10 09:17:55 +0800
committerRyan Dahl <ry@tinyclouds.org>2018-12-09 20:17:55 -0500
commit626cef41b60b3f1802655fd5ea4f638db4934466 (patch)
tree278a6a17382c77aebae5701823718c12c470067e /file_server.ts
parent335343f51fbea2303f904007a5a12b779919918b (diff)
supply the directory to serve as CLI argument (denoland/deno_std#13)
Original: https://github.com/denoland/deno_std/commit/7cd4d9f4eaa4b1d167306b9000af30421319600a
Diffstat (limited to 'file_server.ts')
-rwxr-xr-xfile_server.ts16
1 files changed, 9 insertions, 7 deletions
diff --git a/file_server.ts b/file_server.ts
index 498f9b9d2..9dcac8704 100755
--- a/file_server.ts
+++ b/file_server.ts
@@ -1,20 +1,22 @@
#!/usr/bin/env deno --allow-net
// This program serves files in the current directory over HTTP.
-// TODO Supply the directory to serve as a CLI argument.
// TODO Stream responses instead of reading them into memory.
// TODO Add tests like these:
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
-import { listenAndServe } from "./http.ts";
-import { cwd, readFile, DenoError, ErrorKind } from "deno";
+import { listenAndServe } from "./http";
+import { cwd, readFile, DenoError, ErrorKind, args } from "deno";
const addr = "0.0.0.0:4500";
-const currentDir = cwd();
-
+let currentDir = cwd();
+const target = args[1];
+if (target) {
+ currentDir = `${currentDir}/${target}`;
+}
const encoder = new TextEncoder();
-listenAndServe(addr, async req => {
+listenAndServe(addr, async req => {
const fileName = req.url.replace(/\/$/, '/index.html');
const filePath = currentDir + fileName;
let file;
@@ -25,7 +27,7 @@ listenAndServe(addr, async req => {
if (e instanceof DenoError && e.kind === ErrorKind.NotFound) {
await req.respond({ status: 404, body: encoder.encode("Not found") });
} else {
- await req.response({ status: 500, body: encoder.encode("Internal server error") });
+ await req.respond({ status: 500, body: encoder.encode("Internal server error") });
}
return;
}