mirror of
https://github.com/denoland/deno.git
synced 2024-12-11 18:17:48 -05:00
supply the directory to serve as CLI argument (#13)
This commit is contained in:
parent
721306e726
commit
7cd4d9f4ea
2 changed files with 10 additions and 8 deletions
|
@ -1,17 +1,19 @@
|
||||||
#!/usr/bin/env deno --allow-net
|
#!/usr/bin/env deno --allow-net
|
||||||
|
|
||||||
// This program serves files in the current directory over HTTP.
|
// 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 Stream responses instead of reading them into memory.
|
||||||
// TODO Add tests like these:
|
// TODO Add tests like these:
|
||||||
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
|
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
|
||||||
|
|
||||||
import { listenAndServe } from "./http.ts";
|
import { listenAndServe } from "./http";
|
||||||
import { cwd, readFile, DenoError, ErrorKind } from "deno";
|
import { cwd, readFile, DenoError, ErrorKind, args } from "deno";
|
||||||
|
|
||||||
const addr = "0.0.0.0:4500";
|
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();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
listenAndServe(addr, async req => {
|
listenAndServe(addr, async req => {
|
||||||
|
@ -25,7 +27,7 @@ listenAndServe(addr, async req => {
|
||||||
if (e instanceof DenoError && e.kind === ErrorKind.NotFound) {
|
if (e instanceof DenoError && e.kind === ErrorKind.NotFound) {
|
||||||
await req.respond({ status: 404, body: encoder.encode("Not found") });
|
await req.respond({ status: 404, body: encoder.encode("Not found") });
|
||||||
} else {
|
} else {
|
||||||
await req.response({ status: 500, body: encoder.encode("Internal server error") });
|
await req.respond({ status: 500, body: encoder.encode("Internal server error") });
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
2
http.ts
2
http.ts
|
@ -82,7 +82,7 @@ export async function* serve(addr: string) {
|
||||||
listener.close();
|
listener.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listenAndServe(addr: string, handler: (ServerRequest) => void) {
|
export async function listenAndServe(addr: string, handler: (req: ServerRequest) => void) {
|
||||||
const server = serve(addr);
|
const server = serve(addr);
|
||||||
|
|
||||||
for await (const request of server) {
|
for await (const request of server) {
|
||||||
|
|
Loading…
Reference in a new issue