Skip to content

Instantly share code, notes, and snippets.

@rybla
Created February 10, 2025 22:24
Show Gist options
  • Save rybla/195f3e15d0c70d7d3a577170588f4987 to your computer and use it in GitHub Desktop.
Save rybla/195f3e15d0c70d7d3a577170588f4987 to your computer and use it in GitHub Desktop.
simple HTTP server using Bun
const serve_dir = "docs"
const port = 8000;
Bun.serve({
port,
async fetch(req) {
const url_str = req.url.endsWith("/") ? `${req.url}index.html` : req.url
const url = new URL(url_str);
const filePath = `${serve_dir}${url.pathname}`;
console.log(`GET ${filePath}`)
const file = Bun.file(filePath);
if (!(await file.exists())) return new Response(`Not Found: ${url_str}`, { status: 404 });
return new Response(file);
}
});
console.log(`serving at http://localhost:${port}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment