-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
37 lines (28 loc) · 853 Bytes
/
serve.js
File metadata and controls
37 lines (28 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { join } from 'node:path';
/**
* Start a static file server for the dist/ directory.
* @param {number} [port=3000] - The port to serve on.
* @returns {import('bun').Server} The Bun server instance.
*/
export function startServer(port = 3000) {
const distDir = join(process.cwd(), 'dist');
const server = Bun.serve({
port,
async fetch(req) {
let path = new URL(req.url).pathname;
if (path.endsWith('/')) {
path += 'index.html';
}
if (!path.includes('.')) {
path += '/index.html';
}
const distFile = Bun.file(join(distDir, path));
if (await distFile.exists()) {
return new Response(distFile);
}
return new Response('Not found', { status: 404 });
},
});
console.log(`Serving dist/ at http://localhost:${server.port}`);
return server;
}