Repository navigation
Expand file tree
/
Copy pathserve.js
More file actions
88 lines (72 loc) · 2.75 KB
/
Copy pathserve.js
File metadata and controls
88 lines (72 loc) · 2.75 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { existsSync, readdirSync } from 'fs';
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || "0.0.0.0";
console.log(`Starting server on ${HOST}:${PORT}...`);
console.log(`Working directory: ${process.cwd()}`);
// Check if dist directory exists
const distPath = './dist';
if (existsSync(distPath)) {
console.log(`✅ dist directory exists`);
const files = readdirSync(distPath);
console.log(`dist contents: ${files.join(', ')}`);
// Check for page subdirectories
const einleitung = existsSync('./dist/einleitung');
const surveyTools = existsSync('./dist/survey_tools');
console.log(`einleitung dir exists: ${einleitung}`);
console.log(`survey_tools dir exists: ${surveyTools}`);
if (einleitung) {
console.log(`einleitung contents: ${readdirSync('./dist/einleitung').join(', ')}`);
}
} else {
console.error(`❌ dist directory NOT FOUND at ${distPath}`);
console.log(`Current directory contents:`, readdirSync('.'));
}
const server = Bun.serve({
port: PORT,
hostname: HOST,
async fetch(req) {
const url = new URL(req.url);
let pathname = url.pathname;
console.log(`[${new Date().toISOString()}] ${req.method} ${pathname}`);
// Default to index.html for directory requests
if (pathname.endsWith("/")) {
pathname += "index.html";
}
// Serve files from src/content/snippets for /snippets/ requests
if (pathname.startsWith("/snippets/")) {
const snippetPath = `./src/content/snippets${pathname.slice("/snippets".length)}`;
const snippetFile = Bun.file(snippetPath);
if (await snippetFile.exists()) {
console.log(` → Serving snippet: ${snippetPath}`);
return new Response(snippetFile);
}
}
// Try to serve the file directly
let filePath = `./dist${pathname}`;
let file = Bun.file(filePath);
if (await file.exists()) {
console.log(` → Serving: ${filePath}`);
return new Response(file);
}
// Try with /index.html for clean URLs (e.g., /survey_tools -> /survey_tools/index.html)
if (!pathname.endsWith(".html") && !pathname.endsWith("/")) {
const dirIndexFile = Bun.file(`./dist${pathname}/index.html`);
if (await dirIndexFile.exists()) {
console.log(` → Serving: ./dist${pathname}/index.html`);
return new Response(dirIndexFile);
}
}
// Try with .html extension
if (!pathname.endsWith(".html")) {
const htmlFile = Bun.file(`${filePath}.html`);
if (await htmlFile.exists()) {
console.log(` → Serving: ${filePath}.html`);
return new Response(htmlFile);
}
}
// 404
console.log(` → 404 Not Found: ${pathname}`);
return new Response("Not Found", { status: 404 });
},
});
console.log(`✅ Server running at ${server.url}`);