const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = process.env.PORT || 3000; const PUBLIC_DIR = path.join(__dirname, 'dist'); const getContentType = (filePath) => { const ext = path.extname(filePath).toLowerCase(); const types = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon', }; return types[ext] || 'application/octet-stream'; }; const server = http.createServer((req, res) => { let filePath = path.join(PUBLIC_DIR, req.url === '/' ? 'index.html' : req.url); fs.stat(filePath, (err, stats) => { if (err || !stats.isFile()) { // SPA Fallback: serve index.html for unknown paths filePath = path.join(PUBLIC_DIR, 'index.html'); } fs.readFile(filePath, (err, content) => { if (err) { res.writeHead(500); res.end('Server Error'); } else { res.writeHead(200, { 'Content-Type': getContentType(filePath) }); res.end(content, 'utf-8'); } }); }); }); server.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });