-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
91 lines (79 loc) · 2.88 KB
/
Copy pathserver.js
File metadata and controls
91 lines (79 loc) · 2.88 KB
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const PORT = process.env.PORT || 3000;
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml'
};
const server = http.createServer((req, res) => {
// Gestion CORS pour permettre les requêtes cross-origin
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Token, X-Gtk');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
const parsedUrl = url.parse(req.url);
let pathname = `.${parsedUrl.pathname}`;
// Rediriger / vers /index.html
if (pathname === './') {
pathname = './index.html';
}
// Sécurité: empêcher l'accès aux fichiers en dehors du répertoire
const safeSuffix = path.normalize(pathname).replace(/^(\\.\\.[/\\\\])+/, '');
const fullPath = path.join(__dirname, safeSuffix);
fs.readFile(fullPath, (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// Fichier non trouvé - rediriger vers index.html
fs.readFile('./index.html', (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 - Page non trouvée');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 - Erreur serveur');
}
} else {
const ext = path.parse(fullPath).ext;
const contentType = mimeTypes[ext] || 'text/plain';
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
}
});
});
server.listen(PORT, () => {
console.log(`🚀 EcoleDirecte Exporter démarré sur http://localhost:${PORT}`);
console.log(`📱 Ouvrez votre navigateur à cette adresse`);
console.log(`🛑 Appuyez sur Ctrl+C pour arrêter le serveur`);
});
// Gestion propre de l'arrêt
process.on('SIGINT', () => {
console.log('\n👋 Arrêt du serveur...');
server.close(() => {
console.log('✅ Serveur arrêté proprement');
process.exit(0);
});
});
process.on('SIGTERM', () => {
console.log('\n👋 Arrêt du serveur...');
server.close(() => {
console.log('✅ Serveur arrêté proprement');
process.exit(0);
});
});