-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (58 loc) · 1.79 KB
/
Copy pathserver.js
File metadata and controls
67 lines (58 loc) · 1.79 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
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 3002;
const PUBLIC_DIR = path.join(__dirname, 'build');
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.glb': 'model/gltf-binary',
'.gltf': 'model/gltf+json',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.otf': 'font/otf'
};
const server = http.createServer((req, res) => {
// Normalize path to prevent directory traversal
let filePath = path.join(PUBLIC_DIR, req.url.split('?')[0]);
if (filePath.endsWith(path.sep)) {
filePath = path.join(filePath, 'index.html');
}
const ext = path.extname(filePath).toLowerCase();
fs.stat(filePath, (err, stats) => {
if (err || !stats.isFile()) {
// File not found or is a directory, serve SvelteKit static fallback for SPA routing
const fallbackPath = path.join(PUBLIC_DIR, '404.html');
fs.readFile(fallbackPath, (fbErr, content) => {
if (fbErr) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
}
});
return;
}
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': contentType });
// Stream file for efficiency
const stream = fs.createReadStream(filePath);
stream.pipe(res);
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Felsstudio static server running on port ${PORT}`);
});