Repository navigation
Expand file tree
/
Copy pathdev-server.js
More file actions
181 lines (159 loc) · 5.11 KB
/
Copy pathdev-server.js
File metadata and controls
181 lines (159 loc) · 5.11 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = process.env.PORT || 3000;
const DIST_DIR = path.join(__dirname, 'dist');
// MIME types
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.webp': 'image/webp',
'.ico': 'image/x-icon'
};
// Build the project first
function buildProject() {
return new Promise((resolve, reject) => {
console.log('🔨 Building project...');
exec('node build.js', (error, stdout, stderr) => {
if (error) {
console.error(`Build error: ${error}`);
reject(error);
return;
}
console.log(stdout);
if (stderr) console.error(stderr);
resolve();
});
});
}
// Create HTTP server
function createServer() {
const server = http.createServer((req, res) => {
let filePath = path.join(DIST_DIR, req.url === '/' ? 'index.html' : req.url);
// Security: prevent directory traversal
if (!filePath.startsWith(DIST_DIR)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
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') {
// Try to serve index.html for SPA routes
if (!extname) {
filePath = path.join(DIST_DIR, 'index.html');
fs.readFile(filePath, (error, content) => {
if (error) {
res.writeHead(404);
res.end('404 Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
}
});
} else {
res.writeHead(404);
res.end('404 Not Found');
}
} else {
res.writeHead(500);
res.end(`Server Error: ${error.code}`);
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log(`\n🚀 Development server running at:`);
console.log(` http://localhost:${PORT}`);
console.log(`\n📁 Serving files from: ${DIST_DIR}`);
console.log(`\n👀 Watching for changes...`);
console.log(` Press Ctrl+C to stop\n`);
});
return server;
}
// Watch for file changes
function watchFiles() {
const watchDirs = [
path.join(__dirname, 'images'),
__dirname
];
const rebuild = debounce(async () => {
console.log('\n🔄 Changes detected, rebuilding...');
try {
await buildProject();
console.log('✅ Rebuild complete!\n');
} catch (error) {
console.error('❌ Rebuild failed:', error);
}
}, 500);
// Watch template file
if (fs.existsSync(path.join(__dirname, 'index.template.html'))) {
fs.watch(path.join(__dirname, 'index.template.html'), (eventType) => {
if (eventType === 'change') {
rebuild();
}
});
}
// Watch images directory
fs.watch(path.join(__dirname, 'images'), { recursive: true }, (eventType, filename) => {
if (filename) {
rebuild();
}
});
// Watch build.js
fs.watch(path.join(__dirname, 'build.js'), (eventType) => {
if (eventType === 'change') {
rebuild();
}
});
}
// Debounce function to prevent multiple rebuilds
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Main function
async function main() {
try {
// Initial build
await buildProject();
// Start server
createServer();
// Watch for changes
watchFiles();
} catch (error) {
console.error('Failed to start dev server:', error);
process.exit(1);
}
}
// Handle process termination
process.on('SIGINT', () => {
console.log('\n\n👋 Shutting down development server...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n\n👋 Shutting down development server...');
process.exit(0);
});
// Start the dev server
main();