Last active
January 22, 2024 18:39
-
-
Save ThatBlockyPenguin/fc8d06a1f8a1a6493b9fe6159d20ed0f to your computer and use it in GitHub Desktop.
Basic Deno Server
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
// deno install --allow-read --allow-net https://gist.githubusercontent.com/ThatBlockyPenguin/fc8d06a1f8a1a6493b9fe6159d20ed0f/raw/serve.ts | |
import { existsSync } from 'https://deno.land/std/fs/mod.ts'; | |
import { join } from 'https://deno.land/std/path/mod.ts'; | |
const serveDir = join('./', Deno.args[0]); | |
const dirFound = existsSync(serveDir); | |
if (!dirFound) | |
throw `Directory "${serveDir}" not found!`; | |
console.log(`Listening in directory ${serveDir}`); | |
Deno.serve(async (req) => { | |
const urlPath = new URL(req.url).pathname.substring(1).trim(); | |
const path = join(serveDir, urlPath == '' ? 'index.html' : urlPath); | |
console.log(`Request on ${path}`); | |
return new Response(await Deno.readFile(path)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment