const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = 8081; const server = http.createServer((req, res) => { let filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url); const ext = path.extname(filePath); const contentTypes = { '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript', '.png': 'image/png', '.jpg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon' }; console.log(`[${new Date().toISOString()}] 请求:${req.url} -> ${filePath}`); fs.readFile(filePath, (err, data) => { if (err) { console.error(`文件读取失败:${filePath}`); res.writeHead(404, { 'Content-Type': 'text/html' }); res.end('