-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
121 lines (100 loc) · 3.93 KB
/
serve.js
File metadata and controls
121 lines (100 loc) · 3.93 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Simple HTTP server to serve the AI Transcript Viewer
// This helps avoid browser security restrictions when loading modules
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
// Configuration
const PORT = 9080;
const HOST = 'localhost';
const MAIN_FILE = 'index.html';
// MIME types for common file extensions
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wasm': 'application/wasm'
};
// Create the server
const server = http.createServer((req, res) => {
// Get the URL path (removing query parameters)
let url = req.url.split('?')[0];
// Normalize the URL to prevent path traversal attacks
url = url.replace(/\.\./g, '');
// Default to main file if requesting the root
if (url === '/') {
url = `/${MAIN_FILE}`;
}
// Construct the file path
const filePath = path.join(__dirname, url);
// Check if the file exists
fs.stat(filePath, (err, stats) => {
if (err) {
// If the file doesn't exist, return 404
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1><p>The requested file could not be found.</p>');
return;
}
// Handle directory requests with a listing
if (stats.isDirectory()) {
fs.readdir(filePath, (err, files) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<h1>500 Internal Server Error</h1>');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<h1>Directory Listing</h1>');
res.write('<ul>');
// Add parent directory link
if (url !== '/') {
res.write(`<li><a href="${path.dirname(url)}">../</a></li>`);
}
// List all files and directories
files.forEach(file => {
const isDir = fs.statSync(path.join(filePath, file)).isDirectory();
res.write(`<li><a href="${path.join(url, file)}">${file}${isDir ? '/' : ''}</a></li>`);
});
res.write('</ul>');
res.end();
});
return;
}
// Get the file extension
const ext = path.extname(filePath);
// Set Content-Type header based on file extension
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
// Read and serve the file
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<h1>500 Internal Server Error</h1>');
return;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
});
});
// Start the server
server.listen(PORT, HOST, () => {
const url = `http://${HOST}:${PORT}`;
console.log(`Server running at: ${url}`);
console.log(`Opening ${MAIN_FILE} in your default browser...`);
// Attempt to open the browser automatically
try {
// Try to detect the platform and use the appropriate command
const command = process.platform === 'win32' ? 'start' :
process.platform === 'darwin' ? 'open' : 'xdg-open';
exec(`${command} ${url}`);
} catch (err) {
console.log('Unable to open browser automatically. Please open manually:');
console.log(url);
}
console.log('\nPress Ctrl+C to stop the server');
});