-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-server.js
More file actions
130 lines (111 loc) · 4.51 KB
/
start-server.js
File metadata and controls
130 lines (111 loc) · 4.51 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
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
/**
* Simple HTTP server for running the Transcript Viewer locally
* This allows loading js-tiktoken from node_modules which would be
* blocked when opening the HTML directly due to browser security
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const { exec } = require('child_process');
// Configuration
const PORT = 9080;
const HOST = 'localhost';
const MAIN_FILE = 'index.html';
// Create server
const server = http.createServer((req, res) => {
// Parse URL
const parsedUrl = url.parse(req.url);
let pathname = path.join(__dirname, parsedUrl.pathname);
// Default to index file if request is for root
if (parsedUrl.pathname === '/') {
pathname = path.join(__dirname, MAIN_FILE);
}
// Check if file exists
fs.stat(pathname, (err, stats) => {
if (err) {
// File not found
res.statusCode = 404;
res.end(`File ${parsedUrl.pathname} not found!`);
return;
}
// Check if it's a directory
if (stats.isDirectory()) {
// Redirect to index file if exists
const indexPath = path.join(pathname, MAIN_FILE);
if (fs.existsSync(indexPath)) {
pathname = indexPath;
} else {
// List directory contents
fs.readdir(pathname, (err, files) => {
if (err) {
res.statusCode = 500;
res.end(`Error reading directory: ${err}`);
return;
}
res.setHeader('Content-Type', 'text/html');
res.write('<html><head><title>Directory Listing</title></head><body>');
res.write(`<h1>Directory ${parsedUrl.pathname}</h1><ul>`);
// Add parent directory link
if (parsedUrl.pathname !== '/') {
const parentPath = parsedUrl.pathname.split('/').slice(0, -1).join('/') || '/';
res.write(`<li><a href="${parentPath}">../</a></li>`);
}
// List files
files.forEach(file => {
const isDir = fs.statSync(path.join(pathname, file)).isDirectory();
res.write(`<li><a href="${path.join(parsedUrl.pathname, file)}">${file}${isDir ? '/' : ''}</a></li>`);
});
res.end('</ul></body></html>');
});
return;
}
}
// Read file
fs.readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500;
res.end(`Error reading file: ${err}`);
return;
}
// Get file extension for content type
const ext = path.extname(pathname).toLowerCase();
let contentType = 'text/plain';
// Set content type based on extension
switch (ext) {
case '.html': contentType = 'text/html'; break;
case '.js': contentType = 'text/javascript'; break;
case '.css': contentType = 'text/css'; break;
case '.json': contentType = 'application/json'; break;
case '.png': contentType = 'image/png'; break;
case '.jpg': contentType = 'image/jpeg'; break;
case '.gif': contentType = 'image/gif'; break;
case '.svg': contentType = 'image/svg+xml'; break;
}
// Send response
res.setHeader('Content-Type', contentType);
res.end(data);
});
});
});
// Start server
server.listen(PORT, HOST, () => {
const address = `http://${HOST}:${PORT}`;
console.log(`Server running at ${address}`);
console.log(`Opening browser to ${address}`);
// Open browser
let command;
switch (process.platform) {
case 'darwin': command = `open ${address}`; break;
case 'win32': command = `start ${address}`; break;
default: command = `xdg-open ${address}`; break;
}
exec(command, (err) => {
if (err) {
console.error('Error opening browser:', err);
console.log(`Please open ${address} in your browser.`);
}
});
console.log('\nPress Ctrl+C to stop the server\n');
});