1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00
Original: 108127178b
This commit is contained in:
Bartek Iwańczuk 2018-12-12 10:38:46 +01:00 committed by Ryan Dahl
parent b945303329
commit 0212401974

View file

@ -5,7 +5,7 @@
// 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, ServerRequest, setContentLength } from "./http"; import { listenAndServe, ServerRequest, setContentLength, Response } from "./http";
import { cwd, readFile, DenoError, ErrorKind, args, stat, readDir } from "deno"; import { cwd, readFile, DenoError, ErrorKind, args, stat, readDir } from "deno";
const dirViewerTemplate = ` const dirViewerTemplate = `
@ -143,7 +143,7 @@ async function serveDir(req: ServerRequest, dirPath: string, dirName: string) {
headers headers
}; };
setContentLength(res); setContentLength(res);
await req.respond(res); return res;
} }
async function serveFile(req: ServerRequest, filename: string) { async function serveFile(req: ServerRequest, filename: string) {
@ -156,7 +156,7 @@ async function serveFile(req: ServerRequest, filename: string) {
body: file, body: file,
headers headers
}; };
await req.respond(res); return res;
} }
async function serveFallback(req: ServerRequest, e: Error) { async function serveFallback(req: ServerRequest, e: Error) {
@ -164,31 +164,45 @@ async function serveFallback(req: ServerRequest, e: Error) {
e instanceof DenoError && e instanceof DenoError &&
(e as DenoError<any>).kind === ErrorKind.NotFound (e as DenoError<any>).kind === ErrorKind.NotFound
) { ) {
await req.respond({ status: 404, body: encoder.encode("Not found") }); return {
status: 404,
body: encoder.encode("Not found")
};
} else { } else {
await req.respond({ return {
status: 500, status: 500,
body: encoder.encode("Internal server error") body: encoder.encode("Internal server error")
}); };
} }
} }
function serverLog(req: ServerRequest, res: Response) {
const d = new Date().toISOString();
const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`;
const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`;
console.log(s);
}
listenAndServe(addr, async req => { listenAndServe(addr, async req => {
const fileName = req.url.replace(/\/$/, ""); const fileName = req.url.replace(/\/$/, "");
const filePath = currentDir + fileName; const filePath = currentDir + fileName;
let response: Response;
try { try {
const fileInfo = await stat(filePath); const fileInfo = await stat(filePath);
if (fileInfo.isDirectory()) { if (fileInfo.isDirectory()) {
// Bug with deno.stat: name and path not populated // Bug with deno.stat: name and path not populated
// Yuck! // Yuck!
await serveDir(req, filePath, fileName); response = await serveDir(req, filePath, fileName);
} else { } else {
await serveFile(req, filePath); response = await serveFile(req, filePath);
} }
} catch (e) { } catch (e) {
await serveFallback(req, e); response = await serveFallback(req, e);
return; } finally {
serverLog(req, response);
req.respond(response);
} }
}); });