UNPKG

comic-bubbles

Version:

Animated comic bubbles - what else?

58 lines (49 loc) 1.46 kB
const http = require('http'); const fs = require('fs'); const path = require('path'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { console.log(`Request for ${req.url} received.`); // Default to index.html if the request is '/' const requestedPath = req.url === '/' ? 'index.html' : req.url; const filePath = path.join(__dirname, requestedPath); const ext = path.extname(filePath); const contentTypeMap = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', }; // If the file extension is not in the contentTypeMap, return 404 if (!contentTypeMap[ext]) { res.writeHead(404); res.end('404 Not Found'); return; } fs.readFile(filePath, (err, content) => { if (err) { // File not found if (err.code === 'ENOENT') { res.writeHead(404); res.end('404 Not Found'); } else { // Other error res.writeHead(500); res.end(`Error loading file: ${err}`); } return; } res.writeHead(200, { 'Content-Type': contentTypeMap[ext] }); res.end(content, 'utf-8'); }); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });