const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = process.env.PORT || 24710; const ROOT = __dirname; const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'application/javascript', '.wasm': 'application/wasm', '.data': 'application/octet-stream', '.css': 'text/css', '.png': 'image/png', '.ico': 'image/x-icon', '.svg': 'image/svg+xml', '.json': 'application/json', '.jpg': 'image/jpeg', }; function extType(p) { return MIME[path.extname(p).toLowerCase()] || 'application/octet-stream'; } // 首页:基于原始 index.html,注入启动封面(不修改任何 Unity 加载逻辑) function serveIndex(res) { try { let html = fs.readFileSync(path.join(ROOT, 'index.html'), 'utf-8'); // 注入到 之前的 CSS const css = ` `; // 注入到 最前面的 overlay const overlay = `

运动伤害现场急救虚拟仿真实验

VIRTUAL SIMULATION EXPERIMENT

点击按钮进入全屏沉浸式实验环境

`; // 注入到 之前的 JS(不修改原始 Unity 加载逻辑) const js = ` `; html = html.replace('', css + '\n'); html = html.replace('', '\n' + overlay); html = html.replace('', js + '\n'); res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); res.end(html); } catch (err) { res.writeHead(500); res.end('Internal Server Error'); console.error(err); } } // 静态文件 function serveStatic(req, res) { let fp = path.join(ROOT, req.url.split('?')[0]); if (!fp.startsWith(ROOT)) { res.writeHead(403); res.end('Forbidden'); return; } fs.readFile(fp, (err, data) => { if (err) { res.writeHead(404); res.end('Not Found'); return; } res.writeHead(200, { 'Content-Type': extType(fp) }); res.end(data); }); } http.createServer((req, res) => { if (req.url === '/' || req.url === '/index.html') return serveIndex(res); serveStatic(req, res); }).listen(PORT, () => { console.log(''); console.log(' ╔══════════════════════════════════════════════════╗'); console.log(' ║ 运动伤害现场急救虚拟仿真实验 - WebGL Server ║'); console.log(' ║ ║'); console.log(' ║ Server running at: http://localhost:' + PORT + ' ║'); console.log(' ║ Press Ctrl+C to stop ║'); console.log(' ╚══════════════════════════════════════════════════╝'); console.log(''); });