Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Last active January 3, 2022 07:16
Show Gist options
  • Save mayankchoubey/f4e5702f892d9a1b3a23661fe9f97b92 to your computer and use it in GitHub Desktop.
Save mayankchoubey/f4e5702f892d9a1b3a23661fe9f97b92 to your computer and use it in GitHub Desktop.
Deno content server by reading local files using fetch
import { serve } from "https://deno.land/std/http/mod.ts";
const BASE_PATH = "file:///var/tmp/testdata";
const reqHandler = async (req: Request) => {
const p = new URL(req.url).pathname;
const filePath = BASE_PATH + p;
try {
const res = await fetch(filePath);
return new Response(res.body);
} catch (e) {
if (e instanceof TypeError) {
return new Response(null, { status: 404 });
}
}
return new Response(null, { status: 500 });
};
serve(reqHandler, { port: 8080 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment