fix: add service management scripts for port conflicts

New tools:
- 鍋滄鎵€鏈夋湇鍔?bat - Stop all services
- 鏌ョ湅绔彛鍗犵敤.bat - Check port usage
- 閲嶅惎鏈嶅姟.bat - Restart services
- 蹇€熶慨澶?绔彛鍗犵敤.md - Troubleshooting guide

These scripts help resolve:
- EADDRINUSE error (port 3001 already in use)
- ENOBUFS error (network connection issues)
- Timeout errors (backend not responding)
This commit is contained in:
AI Clinical Dev Team
2025-10-10 21:23:51 +08:00
parent cc3323b795
commit 16b975c340
4 changed files with 373 additions and 0 deletions

56
停止所有服务.bat Normal file
View File

@@ -0,0 +1,56 @@
@echo off
chcp 65001 >nul
echo ========================================
echo 停止所有服务
echo ========================================
echo.
echo [1] 查找占用3001端口的进程...
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :3001') do (
set PID=%%a
echo 找到进程: PID=%%a
taskkill /F /PID %%a >nul 2>&1
if errorlevel 1 (
echo ❌ 无法终止进程 %%a
) else (
echo ✅ 已终止进程 %%a
)
)
echo.
echo [2] 查找占用3000端口的进程...
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :3000') do (
set PID=%%a
echo 找到进程: PID=%%a
taskkill /F /PID %%a >nul 2>&1
if errorlevel 1 (
echo ❌ 无法终止进程 %%a
) else (
echo ✅ 已终止进程 %%a
)
)
echo.
echo [3] 查找Node进程...
tasklist | findstr node.exe >nul
if not errorlevel 1 (
echo 找到Node进程正在终止...
taskkill /F /IM node.exe >nul 2>&1
if errorlevel 1 (
echo ❌ 无法终止Node进程
) else (
echo ✅ 已终止所有Node进程
)
) else (
echo 没有运行中的Node进程
)
echo.
echo ========================================
echo 清理完成
echo ========================================
echo.
echo 现在可以重新启动服务了
echo.
pause

View File

@@ -0,0 +1,246 @@
# 🔧 快速修复:端口占用问题
## ❌ 问题症状
### 错误1EADDRINUSE
```
ERROR: listen EADDRINUSE: address already in use 0.0.0.0:3001
```
**原因:** 后端服务已经在运行端口3001被占用
### 错误2ENOBUFS / 连接错误
```
[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`
### 错误3Docker容器未运行
**解决:**
```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 <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` 的输出
---
**✅ 按照以上步骤操作,问题应该可以解决!**

50
查看端口占用.bat Normal file
View File

@@ -0,0 +1,50 @@
@echo off
chcp 65001 >nul
echo ========================================
echo 查看端口占用情况
echo ========================================
echo.
echo [端口 3001 - 后端服务]
netstat -ano | findstr :3001
if errorlevel 1 (
echo ✅ 端口3001空闲
) else (
echo ⚠️ 端口3001已被占用
echo.
echo 占用进程详情:
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :3001 ^| findstr LISTENING') do (
tasklist | findstr %%a
)
)
echo.
echo [端口 3000 - 前端服务]
netstat -ano | findstr :3000
if errorlevel 1 (
echo ✅ 端口3000空闲
) else (
echo ⚠️ 端口3000已被占用
echo.
echo 占用进程详情:
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :3000 ^| findstr LISTENING') do (
tasklist | findstr %%a
)
)
echo.
echo [所有Node.js进程]
tasklist | findstr node.exe
if errorlevel 1 (
echo 没有运行中的Node进程
)
echo.
echo ========================================
echo 检查完成
echo ========================================
echo.
echo 如需停止服务,请运行: 停止所有服务.bat
echo.
pause

21
重启服务.bat Normal file
View File

@@ -0,0 +1,21 @@
@echo off
chcp 65001 >nul
echo ========================================
echo 重启服务
echo ========================================
echo.
echo [1/3] 停止所有现有服务...
call "%~dp0停止所有服务.bat"
echo.
echo [2/3] 等待2秒...
timeout /t 2 /nobreak >nul
echo.
echo [3/3] 启动服务...
call "%~dp0一键启动.bat"
echo.
echo 重启完成!