const os = require('os'); const http = require('http'); // 获取系统信息 const getSystemInfo = () => { try { const totalMem = (os.totalmem() / (1024 ** 3)).toFixed(2) + ' GB'; const freeMem = (os.freemem() / (1024 ** 3)).toFixed(2) + ' GB'; const usedMem = ((os.totalmem() - os.freemem()) / (1024 ** 3)).toFixed(2) + ' GB'; const memUsage = (((os.totalmem() - os.freemem()) / os.totalmem()) * 100).toFixed(2) + '%'; const cpus = os.cpus(); const cpuModel = cpus[0]?.model || 'Unknown'; const cpuCores = cpus.length; return { hostname: os.hostname(), platform: os.platform(), arch: os.arch(), uptime: (os.uptime() / 3600).toFixed(2) + ' hours', // 内存信息:使用前面计算好的变量 totalMem, // 总内存大小 usedMem, // 已使用内存 freeMem, // 空闲内存 memUsage, // 内存使用率 // CPU信息:使用前面计算好的变量 cpuModel, // CPU型号 cpuCores // CPU核心数 }; } catch (error) { console.error('获取系统信息失败:', error.message); return {}; } }; // 创建HTTP服务器 const server = http.createServer((req, res) => { if (req.url === '/') { const systemInfo = getSystemInfo(); const html = `