Skip to content

Instantly share code, notes, and snippets.

@mountainash
Created January 31, 2025 11:25
Show Gist options
  • Save mountainash/f395afa36d2a5607f3f165b921a77387 to your computer and use it in GitHub Desktop.
Save mountainash/f395afa36d2a5607f3f165b921a77387 to your computer and use it in GitHub Desktop.
Simple Bun.JS Static file server
// Use to serve static files
// Run with `bun server.ts`
const BASE_PATH = './';
Bun.serve({
port: 3000,
async fetch(req) {
const path = new URL(req.url).pathname;
const filePath = `${BASE_PATH}${path === '/' ? '/index.html' : path}`;
const file = Bun.file(filePath);
return new Response(file);
},
error() {
return new Response(null, { status: 404 });
},
});
console.info('🚀 Server running at http://localhost:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment