// server.js const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = 3000; const MIME_TYPES = { '.html': 'text/html', '.css': 'text/css', '.js': 'text/javascript', '.json': 'application/json', '.xml': 'application/xml' }; const server = http.createServer((req, res) => { // Convert URL to file path, using index.html for root let filePath = req.url === '/' ? './index.html' : '.' + req.url; // Get file extension for MIME type const ext = path.extname(filePath); const contentType = MIME_TYPES[ext] || 'text/plain'; // Read and serve the file fs.readFile(filePath, (err, content) => { if (err) { if (err.code === 'ENOENT') { res.writeHead(404); res.end('File not found'); } else { res.writeHead(500); res.end('Server error: ' + err.code); } } else { res.writeHead(200, { 'Content-Type': contentType }); res.end(content); } }); }); server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });