this repo has no description
at main 36 lines 861 B view raw
1const http = require("http"); 2const fs = require("fs"); 3const path = require("path"); 4 5const PORT = 8000; 6 7const MIME_TYPES = { 8 ".html": "text/html", 9 ".css": "text/css", 10 ".js": "text/javascript", 11 ".ico": "image/x-icon", 12 ".svg": "image/svg+xml", 13 ".ttf": "font/ttf", 14}; 15 16http 17 .createServer((req, res) => { 18 let filePath = req.url === "/" ? "/index.html" : req.url; 19 filePath = path.join(__dirname, filePath); 20 21 const ext = path.extname(filePath); 22 const contentType = MIME_TYPES[ext] || "application/octet-stream"; 23 24 fs.readFile(filePath, (err, data) => { 25 if (err) { 26 res.writeHead(404); 27 res.end("Not found"); 28 return; 29 } 30 res.writeHead(200, { "Content-Type": contentType }); 31 res.end(data); 32 }); 33 }) 34 .listen(PORT, () => { 35 console.log(`Serving at http://localhost:${PORT}`); 36 });