-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (81 loc) · 3.21 KB
/
server.js
File metadata and controls
92 lines (81 loc) · 3.21 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
const http = require('http');
const url = require('url');
let currentHTML = '<!DOCTYPE html><html><head><title>Codify - No website generated yet</title></head><body><h1>Waiting for website...</h1></body></html>';
let pages = {};
const PORT = 8765;
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
if (req.method === 'POST' && parsedUrl.pathname === '/update') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const data = JSON.parse(body);
if (data.pages && Array.isArray(data.pages)) {
pages = {};
data.pages.forEach(page => {
if (page.name && page.html) {
pages[page.name] = page.html;
console.log(`[Server] Registered page: ${page.name}`);
}
});
currentHTML = pages['index.html'] || pages[Object.keys(pages)[0]] || currentHTML;
console.log(`[Server] Multi-page site updated. Total pages: ${Object.keys(pages).length}`);
console.log(`[Server] Available pages: ${Object.keys(pages).join(', ')}`);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, pages: Object.keys(pages) }));
}
else if (data.html) {
currentHTML = data.html;
pages = { 'index.html': data.html };
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'No HTML or pages provided' }));
}
} catch (e) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
} else if (req.method === 'GET') {
if (parsedUrl.pathname === '/favicon.ico') {
res.writeHead(204, { 'Content-Type': 'image/x-icon' });
res.end();
return;
}
const pathname = parsedUrl.pathname;
let htmlToServe = currentHTML;
if (pathname === '/' || pathname === '/index.html') {
htmlToServe = pages['index.html'] || currentHTML;
console.log(`[Server] Serving: / (index.html)`);
} else if (pathname.startsWith('/')) {
const pageName = pathname.substring(1);
htmlToServe = pages[pageName] || currentHTML;
if (pages[pageName]) {
console.log(`[Server] Serving: ${pathname} (${pageName})`);
} else {
console.log(`[Server] Page not found: ${pathname}, serving default`);
}
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(htmlToServe);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
});
server.listen(PORT, 'localhost', () => {
console.log(`Codify server running at http://localhost:${PORT}`);
console.log('Keep this server running while using the extension.');
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${PORT} is already in use. The server may already be running.`);
} else {
console.error('Server error:', err);
}
});