/** * Simple HTTP Server for Embryo Grading System * Node.js server to serve the application */ import http from 'http'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PORT = 8000; // MIME types for different file extensions const mimeTypes = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon', '.wasm': 'application/wasm', '.onnx': 'application/octet-stream', '.pt': 'application/octet-stream', '.safetensors': 'application/octet-stream' }; const server = http.createServer((req, res) => { console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`); // Parse URL and remove query string let filePath = '.' + req.url.split('?')[0]; // Default to login.html instead of index.html if (filePath === './') { filePath = './login.html'; } // Get file extension const extname = String(path.extname(filePath)).toLowerCase(); const contentType = mimeTypes[extname] || 'application/octet-stream'; // Read and serve file fs.readFile(filePath, (error, content) => { if (error) { if (error.code === 'ENOENT') { // File not found res.writeHead(404, { 'Content-Type': 'text/html' }); res.end('

404 - File Not Found

', 'utf-8'); } else { // Server error res.writeHead(500); res.end(`Server Error: ${error.code}`, 'utf-8'); } } else { // Success res.writeHead(200, { 'Content-Type': contentType, 'Access-Control-Allow-Origin': '*' }); res.end(content, 'utf-8'); } }); }); server.listen(PORT, () => { console.log('╔════════════════════════════════════════════════════════╗'); console.log('║ Embryo Grading System - Server Running ║'); console.log('╚════════════════════════════════════════════════════════╝'); console.log(''); console.log(` 🚀 Server running at: http://localhost:${PORT}`); console.log(` 📁 Serving files from: ${__dirname}`); console.log(''); console.log(' Press Ctrl+C to stop the server'); console.log(''); console.log('─────────────────────────────────────────────────────────'); }); // Handle server errors server.on('error', (err) => { if (err.code === 'EADDRINUSE') { console.error(`\n❌ Error: Port ${PORT} is already in use.`); console.error(' Please stop the other server or use a different port.\n'); } else { console.error('\n❌ Server error:', err); } process.exit(1); }); // Graceful shutdown process.on('SIGINT', () => { console.log('\n\n🛑 Shutting down server...'); server.close(() => { console.log('✅ Server stopped successfully\n'); process.exit(0); }); });