-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (84 loc) · 2.64 KB
/
server.js
File metadata and controls
98 lines (84 loc) · 2.64 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const { URL } = require('url');
const PORT = 65530;
const DIST_DIR = path.join(__dirname, 'dist');
// MIME types
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.eot': 'application/vnd.ms-fontobject'
};
const server = http.createServer((req, res) => {
// Parse URL
const parsedUrl = new URL(req.url, `http://localhost:${PORT}`);
let pathname = parsedUrl.pathname;
// Handle favicon.ico requests gracefully
if (pathname === '/favicon.ico') {
res.writeHead(204, { 'Content-Type': 'image/x-icon' });
res.end();
return;
}
// Default to index.html
if (pathname === '/') {
pathname = '/index.html';
}
// Security: prevent directory traversal
const safePath = path.normalize(pathname).replace(/^(\.\.[\/\\])+/, '');
const filePath = path.join(DIST_DIR, safePath);
// Check if file exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
return;
}
// Read and serve file
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error');
return;
}
// Get MIME type
const ext = path.extname(filePath).toLowerCase();
const contentType = mimeTypes[ext] || 'application/octet-stream';
// Set headers
res.writeHead(200, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Cache-Control': 'no-cache'
});
res.end(data);
});
});
});
server.listen(PORT, () => {
console.log(`🚀 DeskMaster web server running at http://localhost:${PORT}`);
console.log(`📁 Serving files from: ${DIST_DIR}`);
console.log(`\n⚠️ Note: This is a static file server.`);
console.log(` Electron IPC and system features won't work in a browser.`);
console.log(` Press Ctrl+C to stop the server.\n`);
});
// Handle errors
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`❌ Port ${PORT} is already in use. Please use a different port.`);
} else {
console.error('❌ Server error:', err);
}
process.exit(1);
});