-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (90 loc) · 3.44 KB
/
server.js
File metadata and controls
101 lines (90 loc) · 3.44 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const os = require('os');
const PORT = 3000;
const HOST = '0.0.0.0'; // Listen on all network interfaces
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
const server = http.createServer((req, res) => {
console.log(`request: ${req.url}`);
// Parse URL to ignore query strings (e.g., ?v=2.5)
// using a dummy base since we only care about the path
const parsedUrl = new URL(req.url, 'http://localhost');
let filePath = '.' + parsedUrl.pathname;
if (filePath === './') {
filePath = './index.html';
}
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
fs.readFile('./404.html', (error, content) => {
if (error) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1>', 'utf-8');
} else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
}
});
} else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
}
} else {
// Disable caching for development
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
function getAllLocalIPs() {
const ips = [];
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
// Skip internal (non-127.0.0.1) and non-ipv4 addresses
if ('IPv4' !== iface.family || iface.internal) {
continue;
}
ips.push({ name, address: iface.address });
}
}
return ips;
}
server.listen(PORT, HOST, () => {
const ips = getAllLocalIPs();
console.log(`\n=======================================================`);
console.log(`✅ Server running at http://${HOST}:${PORT}/`);
console.log(`\n📱 Available Network Addresses (Use one of these):`);
if (ips.length > 0) {
ips.forEach(ip => {
console.log(` - ${ip.name}: http://${ip.address}:${PORT}`);
});
console.log(`\n👉 Enter one of the IPs above in the "Server IP" box.`);
} else {
console.log(` (No external network interfaces found. You may be offline.)`);
}
console.log(`\n⚠️ Keep this terminal open while sharing.`);
console.log(`=======================================================\n`);
});