-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (42 loc) · 1.3 KB
/
index.js
File metadata and controls
49 lines (42 loc) · 1.3 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
import { serve } from 'bun';
import { readFile } from 'fs/promises';
import { join } from 'path';
// Define the path for the HTML file
const htmlFilePath = join(import.meta.dir, 'index.html');
// Function to serve the HTML file
async function serveHtmlFile() {
try {
const fileContents = await readFile(htmlFilePath);
return new Response(fileContents, {
headers: {
'Content-Type': 'text/html',
},
});
} catch (error) {
return new Response('File not found', { status: 404 });
}
}
// BunJS Server to serve ping data on '/' and HTML on '/test'
const server = serve({
fetch(req) {
const url = new URL(req.url);
// Serve ping data when requesting the root path "/"
if (url.pathname === '/') {
return new Response(Date.now().toString(), {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}
// Serve the HTML file when requesting "/test"
if (url.pathname === '/test') {
return serveHtmlFile();
}
// Return 404 for any other routes
return new Response('Not found', { status: 404 });
},
port: 80, // Set to port 80 for serving pings and HTML
});
console.log(`Server running at http://localhost`);