Created
January 31, 2025 11:25
-
-
Save mountainash/f395afa36d2a5607f3f165b921a77387 to your computer and use it in GitHub Desktop.
Simple Bun.JS Static file 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
// 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