详细说明: - 添加完整的项目介绍和架构说明 - 包含 7 个 AI Agent 的详细介绍 - 添加技术栈和 Mermaid 架构图 - 完善安装和使用指南 - 添加开发指南和 Git 工作流 - 新增 Windows 快速部署文档 - 更新前端组件和启动脚本 - 添加多种环境配置文件 修改的文件: - 新增 README.md 主文档 - 新增 doc/ 目录及部署文档 - 更新前端演示系统组件 - 添加多个启动脚本变体 - 配置文件优化 影响的功能模块: - 项目文档体系 - 部署和启动流程 - 前端展示系统 - 环境配置管理
138 lines
3.5 KiB
JavaScript
138 lines
3.5 KiB
JavaScript
const http = require('http');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const url = require('url');
|
||
|
||
const PORT = 4155;
|
||
const HOST = '0.0.0.0';
|
||
|
||
// MIME 类型映射
|
||
const mimeTypes = {
|
||
'.html': 'text/html; charset=utf-8',
|
||
'.css': 'text/css',
|
||
'.js': 'text/javascript',
|
||
'.json': 'application/json',
|
||
'.png': 'image/png',
|
||
'.jpg': 'image/jpeg',
|
||
'.jpeg': 'image/jpeg',
|
||
'.gif': 'image/gif',
|
||
'.svg': 'image/svg+xml',
|
||
'.ico': 'image/x-icon',
|
||
'.woff': 'font/woff',
|
||
'.woff2': 'font/woff2',
|
||
'.ttf': 'font/ttf',
|
||
'.eot': 'font/eot'
|
||
};
|
||
|
||
// 创建服务器
|
||
const server = http.createServer((req, res) => {
|
||
// 记录请求
|
||
const timestamp = new Date().toLocaleString('zh-CN');
|
||
const clientIP = req.connection.remoteAddress || req.socket.remoteAddress;
|
||
console.log(`[${timestamp}] ${clientIP} -> ${req.method} ${req.url}`);
|
||
|
||
// 解析 URL
|
||
const parsedUrl = url.parse(req.url);
|
||
let pathname = decodeURIComponent(parsedUrl.pathname);
|
||
|
||
// 默认文件
|
||
if (pathname === '/') {
|
||
pathname = '/index.html';
|
||
}
|
||
|
||
// 构建文件路径
|
||
const filePath = path.join(__dirname, pathname);
|
||
|
||
// 安全检查:确保文件路径在当前目录内
|
||
if (!filePath.startsWith(__dirname)) {
|
||
res.writeHead(403);
|
||
res.end('Forbidden');
|
||
return;
|
||
}
|
||
|
||
// 检查文件是否存在
|
||
fs.stat(filePath, (err, stats) => {
|
||
if (err) {
|
||
// 文件不存在
|
||
res.writeHead(404);
|
||
res.end('Not Found');
|
||
return;
|
||
}
|
||
|
||
if (stats.isDirectory()) {
|
||
// 如果是目录,尝试加载 index.html
|
||
const indexPath = path.join(filePath, 'index.html');
|
||
fs.stat(indexPath, (err, stats) => {
|
||
if (err) {
|
||
res.writeHead(404);
|
||
res.end('Not Found');
|
||
return;
|
||
}
|
||
serveFile(indexPath, res);
|
||
});
|
||
} else {
|
||
// 提供文件
|
||
serveFile(filePath, res);
|
||
}
|
||
});
|
||
});
|
||
|
||
// 提供文件的函数
|
||
function serveFile(filePath, res) {
|
||
const ext = path.extname(filePath);
|
||
const contentType = mimeTypes[ext] || 'application/octet-stream';
|
||
|
||
fs.readFile(filePath, (err, content) => {
|
||
if (err) {
|
||
res.writeHead(500);
|
||
res.end('Internal Server Error');
|
||
return;
|
||
}
|
||
|
||
res.writeHead(200, { 'Content-Type': contentType });
|
||
res.end(content);
|
||
});
|
||
}
|
||
|
||
// 获取本地IP地址
|
||
function getLocalIP() {
|
||
const os = require('os');
|
||
const interfaces = os.networkInterfaces();
|
||
|
||
for (const name of Object.keys(interfaces)) {
|
||
for (const iface of interfaces[name]) {
|
||
// 跳过内部(即127.0.0.1)和非IPv4地址
|
||
if (iface.family === 'IPv4' && !iface.internal) {
|
||
return iface.address;
|
||
}
|
||
}
|
||
}
|
||
return 'localhost';
|
||
}
|
||
|
||
// 启动服务器
|
||
server.listen(PORT, HOST, () => {
|
||
const localIP = getLocalIP();
|
||
|
||
console.log('======================================');
|
||
console.log(' Web Result 静态服务器启动成功');
|
||
console.log('======================================');
|
||
console.log('');
|
||
console.log('🏠 本地访问:');
|
||
console.log(` http://localhost:${PORT}`);
|
||
console.log('');
|
||
|
||
if (localIP !== 'localhost') {
|
||
console.log('🌐 局域网访问:');
|
||
console.log(` http://${localIP}:${PORT}`);
|
||
console.log('');
|
||
console.log('📱 移动设备访问:');
|
||
console.log(` http://${localIP}:${PORT}`);
|
||
console.log('');
|
||
}
|
||
|
||
console.log('[提示] 服务器监听所有网络接口 (0.0.0.0)');
|
||
console.log('[提示] 按 Ctrl+C 停止服务器');
|
||
console.log('');
|
||
console.log('等待请求...');
|
||
}); |