Last active
January 3, 2022 07:16
-
-
Save mayankchoubey/f4e5702f892d9a1b3a23661fe9f97b92 to your computer and use it in GitHub Desktop.
Deno content server by reading local files using fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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