-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·87 lines (77 loc) · 2.99 KB
/
server.js
File metadata and controls
executable file
·87 lines (77 loc) · 2.99 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
#!/usr/bin/env node
/**
* Simple HTTP server for Dithering Studio
* Run with: node server.js
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = process.env.PORT || 3001;
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
};
const server = http.createServer((req, res) => {
// Get the file path
let filePath = '.' + req.url;
if (filePath === './') {
filePath = './index.html';
}
// Get the file extension
const extname = String(path.extname(filePath)).toLowerCase();
const mimeType = mimeTypes[extname] || 'application/octet-stream';
// Read and serve the file
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - File Not Found</h1>', 'utf-8');
} else {
res.writeHead(500);
res.end(`Server Error: ${error.code}`, 'utf-8');
}
} else {
res.writeHead(200, {
'Content-Type': mimeType,
'Access-Control-Allow-Origin': '*',
});
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log(`
╔═══════════════════════════════════════════════════╗
║ 🎨 Dithering Studio Server 🎨 ║
╠═══════════════════════════════════════════════════╣
║ ║
║ Server running at: ║
║ → http://localhost:${PORT} ║
║ ║
║ Press Ctrl+C to stop the server ║
║ ║
╚═══════════════════════════════════════════════════╝
`);
// Open browser automatically
const start = process.platform === 'darwin' ? 'open' :
process.platform === 'win32' ? 'start' : 'xdg-open';
exec(`${start} http://localhost:${PORT}`);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`\n❌ Port ${PORT} is already in use. Please close other applications using this port or change the PORT variable in server.js\n`);
} else {
console.error('\n❌ Server error:', err);
}
process.exit(1);
});