Created
February 10, 2025 22:24
-
-
Save rybla/195f3e15d0c70d7d3a577170588f4987 to your computer and use it in GitHub Desktop.
simple HTTP server using Bun
This file contains 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
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