File size: 3,673 Bytes
ed9f15f |
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 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/**
* 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('<h1>404 - File Not Found</h1>', '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);
});
});
|