-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
23 lines (20 loc) · 707 Bytes
/
Copy pathserver.mjs
File metadata and controls
23 lines (20 loc) · 707 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import http from 'http';
import fs from 'fs';
import path from 'path';
const PORT = 3000;
http.createServer((req, res) => {
let filePath = '.' + (req.url === '/' ? '/index.html' : req.url);
const ext = path.extname(filePath);
const contentType = ext === '.json' ? 'application/json' : ext === '.js' ? 'text/javascript' : 'text/html';
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end("Fichier non trouvé");
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
}
});
}).listen(PORT, () => {
console.log(`🚀 Serveur prêt : http://localhost:${PORT}`);
});