|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
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}`);
|
|
|
|
|
|
|
|
|
let filePath = '.' + req.url.split('?')[0];
|
|
|
|
|
|
|
|
|
if (filePath === './') {
|
|
|
filePath = './login.html';
|
|
|
}
|
|
|
|
|
|
|
|
|
const extname = String(path.extname(filePath)).toLowerCase();
|
|
|
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
|
|
|
|
|
|
|
|
fs.readFile(filePath, (error, content) => {
|
|
|
if (error) {
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
|
|
res.writeHead(404, { 'Content-Type': 'text/html' });
|
|
|
res.end('<h1>404 - File Not Found</h1>', 'utf-8');
|
|
|
} else {
|
|
|
|
|
|
res.writeHead(500);
|
|
|
res.end(`Server Error: ${error.code}`, 'utf-8');
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
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('βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
|
|
|
});
|
|
|
|
|
|
|
|
|
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);
|
|
|
});
|
|
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
console.log('\n\nπ Shutting down server...');
|
|
|
server.close(() => {
|
|
|
console.log('β
Server stopped successfully\n');
|
|
|
process.exit(0);
|
|
|
});
|
|
|
});
|
|
|
|