Skip to content

Instantly share code, notes, and snippets.

@ThatBlockyPenguin
Last active January 22, 2024 18:39
Show Gist options
  • Save ThatBlockyPenguin/fc8d06a1f8a1a6493b9fe6159d20ed0f to your computer and use it in GitHub Desktop.
Save ThatBlockyPenguin/fc8d06a1f8a1a6493b9fe6159d20ed0f to your computer and use it in GitHub Desktop.
Basic Deno Server
// 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