67 lines
2.0 KiB
JavaScript
Executable File
67 lines
2.0 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
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 = 5173;
|
|
const HOST = '0.0.0.0';
|
|
|
|
// Types MIME
|
|
const mimeTypes = {
|
|
'.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'
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
|
|
|
|
let filePath = '.' + req.url;
|
|
if (filePath === './') {
|
|
filePath = './index.html';
|
|
}
|
|
|
|
const extname = String(path.extname(filePath)).toLowerCase();
|
|
const mimeType = mimeTypes[extname] || 'application/octet-stream';
|
|
|
|
fs.readFile(filePath, (error, content) => {
|
|
if (error) {
|
|
if (error.code === 'ENOENT') {
|
|
// Fichier non trouvé, servir index.html pour SPA
|
|
fs.readFile('./index.html', (error, content) => {
|
|
if (error) {
|
|
res.writeHead(404);
|
|
res.end('File not found');
|
|
} else {
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
res.end(content, 'utf-8');
|
|
}
|
|
});
|
|
} else {
|
|
res.writeHead(500);
|
|
res.end('Server error: ' + error.code);
|
|
}
|
|
} else {
|
|
res.writeHead(200, { 'Content-Type': mimeType });
|
|
res.end(content, 'utf-8');
|
|
}
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, HOST, () => {
|
|
console.log(`🚀 Serveur 4NK_IA_front démarré sur http://${HOST}:${PORT}`);
|
|
console.log(`📁 Servant les fichiers depuis: ${process.cwd()}`);
|
|
console.log(`💡 Appuyez sur Ctrl+C pour arrêter`);
|
|
});
|