Spaces:
Runtime error
Runtime error
| import http from 'http'; | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| import { fileURLToPath } from 'url'; | |
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | |
| const PORT = process.env.PORT || 7860; | |
| const DIST_DIR = path.join(__dirname, 'dist'); | |
| const MIME_TYPES = { | |
| '.html': 'text/html', | |
| '.css': 'text/css', | |
| '.js': 'application/javascript', | |
| '.mjs': 'application/javascript', | |
| '.json': 'application/json', | |
| '.png': 'image/png', | |
| '.jpg': 'image/jpeg', | |
| '.gif': 'image/gif', | |
| '.svg': 'image/svg+xml', | |
| '.wasm': 'application/wasm', | |
| '.ico': 'image/x-icon' | |
| }; | |
| const server = http.createServer((req, res) => { | |
| // Add COOP/COEP headers for WASM multithreading | |
| res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); | |
| res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); | |
| // Clean up url path | |
| let filePath = path.join(DIST_DIR, req.url.split('?')[0]); | |
| if (filePath === DIST_DIR || filePath.endsWith('/')) { | |
| filePath = path.join(filePath, 'index.html'); | |
| } | |
| const ext = path.extname(filePath); | |
| const contentType = MIME_TYPES[ext] || 'application/octet-stream'; | |
| fs.readFile(filePath, (err, content) => { | |
| if (err) { | |
| if (err.code === 'ENOENT') { | |
| // Fallback to index.html for SPA routing | |
| fs.readFile(path.join(DIST_DIR, 'index.html'), (errHtml, htmlContent) => { | |
| if (errHtml) { | |
| res.writeHead(500); | |
| res.end('Error loading index.html'); | |
| } else { | |
| res.writeHead(200, { 'Content-Type': 'text/html' }); | |
| res.end(htmlContent, 'utf-8'); | |
| } | |
| }); | |
| } else { | |
| res.writeHead(500); | |
| res.end(`Server Error: ${err.code}`); | |
| } | |
| } else { | |
| res.writeHead(200, { 'Content-Type': contentType }); | |
| res.end(content, 'utf-8'); | |
| } | |
| }); | |
| }); | |
| server.listen(PORT, '0.0.0.0', () => { | |
| console.log(`Static server running on http://0.0.0.0:${PORT}`); | |
| }); | |