Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (69 loc) · 2.31 KB
/
Copy pathserver.js
File metadata and controls
81 lines (69 loc) · 2.31 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
// ============================================================
// server.js — Dev server with static files + game state endpoint
// Usage: node server.js
// ============================================================
import { createServer } from 'http';
import { readFile, stat } from 'fs/promises';
import { join, extname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const PORT = 3000;
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
let gameState = null;
const server = createServer(async (req, res) => {
// --- API: game state ---
if (req.url === '/api/state') {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
return res.end();
}
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
try {
gameState = JSON.parse(body);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end('{"ok":true}');
} catch {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end('{"error":"invalid json"}');
}
});
return;
}
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(gameState, null, 2));
return;
}
}
// --- Static files ---
let filePath = join(__dirname, req.url === '/' ? 'index.html' : req.url);
try {
const info = await stat(filePath);
if (info.isDirectory()) filePath = join(filePath, 'index.html');
const data = await readFile(filePath);
const ext = extname(filePath);
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
res.end(data);
} catch {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
server.listen(PORT, () => {
console.log(`Dev server: http://localhost:${PORT}`);
});