# 🔧 快速修复:端口占用问题 ## ❌ 问题症状 ### 错误1:EADDRINUSE ``` ERROR: listen EADDRINUSE: address already in use 0.0.0.0:3001 ``` **原因:** 后端服务已经在运行,端口3001被占用 ### 错误2:ENOBUFS / 连接错误 ``` [vite] http proxy error: /api/v1/projects AggregateError [ENOBUFS] ``` **原因:** 前端尝试连接后端,但后端未正常运行 ### 错误3:超时错误 ``` AxiosError: timeout of 30000ms exceeded ``` **原因:** API请求超时,后端服务不可用 --- ## ✅ 解决方案(3步) ### 方案1:使用脚本(推荐)⭐ **步骤1:停止所有服务** ```powershell 双击运行:停止所有服务.bat ``` **步骤2:重新启动** ```powershell 双击运行:一键启动.bat ``` --- ### 方案2:手动操作 **步骤1:查看端口占用** ```powershell 双击运行:查看端口占用.bat ``` **步骤2:手动停止进程** ```powershell # 查找占用3001端口的进程 netstat -ano | findstr :3001 # 假设PID是12345,停止该进程 taskkill /F /PID 12345 ``` **步骤3:重新启动** ```powershell # 终端1 - 后端 cd backend npm run dev # 终端2 - 前端 cd frontend npm run dev ``` --- ### 方案3:停止所有Node进程(彻底清理) ```powershell # 停止所有Node进程 taskkill /F /IM node.exe # 然后重新启动 双击运行:一键启动.bat ``` ⚠️ **警告:** 这会停止电脑上所有Node进程,包括其他项目! --- ## 🔍 诊断工具 ### 1. 查看端口占用 ```powershell 查看端口占用.bat ``` ### 2. 停止所有服务 ```powershell 停止所有服务.bat ``` ### 3. 系统诊断 ```powershell 诊断问题.bat ``` --- ## 📋 完整启动清单 ### ✅ 正确的启动流程 1. **确保之前的服务已停止** ```powershell 停止所有服务.bat ``` 2. **启动Docker容器** ```powershell docker-compose up -d # 或者一键启动会自动处理 ``` 3. **启动后端服务** ```powershell cd backend npm run dev ``` **预期输出:** ``` 🚀 AI临床研究平台 - 后端服务器启动成功! 📍 服务地址: http://localhost:3001 ``` 4. **启动前端服务** ```powershell cd frontend npm run dev ``` **预期输出:** ``` ➜ Local: http://localhost:3000/ ``` 5. **访问系统** ``` http://localhost:3000/ ``` --- ## ⚠️ 常见错误 ### 错误1:端口3001已占用 **解决:** 运行 `停止所有服务.bat` ### 错误2:端口3000已占用 **解决:** 运行 `停止所有服务.bat` ### 错误3:Docker容器未运行 **解决:** ```powershell docker-compose up -d ``` ### 错误4:数据库连接失败 **解决:** ```powershell # 检查容器状态 docker ps # 如果没有看到postgres和redis,启动它们 docker-compose up -d ``` --- ## 🎯 快速命令参考 ### 查看端口占用 ```powershell # 查看3001端口 netstat -ano | findstr :3001 # 查看3000端口 netstat -ano | findstr :3000 # 查看所有Node进程 tasklist | findstr node.exe ``` ### 停止进程 ```powershell # 停止特定PID taskkill /F /PID # 停止所有Node进程 taskkill /F /IM node.exe ``` ### 检查服务状态 ```powershell # 后端健康检查 curl http://localhost:3001/health # Docker容器状态 docker ps ``` --- ## 💡 避免端口占用的建议 ### 1. 使用统一的启动脚本 始终使用 `一键启动.bat` 启动服务 ### 2. 使用统一的停止脚本 不用时运行 `停止所有服务.bat` 停止服务 ### 3. 不要重复启动 在启动新服务前,先确保旧服务已停止 ### 4. 关闭终端时确保进程已停止 不要直接关闭终端窗口,先按 `Ctrl+C` 停止服务 --- ## 🆘 仍然无法解决? ### 检查清单 - [ ] 运行了 `停止所有服务.bat` - [ ] 确认端口已释放(`netstat -ano | findstr :3001`) - [ ] Docker容器正在运行(`docker ps`) - [ ] 后端依赖已安装(`cd backend && npm install`) - [ ] 前端依赖已安装(`cd frontend && npm install`) - [ ] 数据库迁移已完成(`cd backend && npx prisma migrate dev`) - [ ] 模拟用户已创建(`cd backend && npx tsx src/scripts/create-mock-user.ts`) ### 获取帮助 如果问题仍未解决,请提供: 1. `查看端口占用.bat` 的输出 2. 后端启动的完整日志 3. 前端启动的完整日志 4. `docker ps` 的输出 --- **✅ 按照以上步骤操作,问题应该可以解决!**