You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 后端启动指南
## 环境要求
- Node.js 20.x LTS
- MySQL 8.0+
- Redis 7.x (可选,未安装时会自动使用内存缓存)
## 快速启动步骤
### 1. 安装依赖
```bash
cd app/backend
npm install
```
### 2. 数据库配置(已配置)
数据库连接信息已配置在 `.env` 文件中:
```env
DATABASE_URL = mysql://root:1qazse42W3@192.168.0.222:3306/aguzhitou
```
** 注意**:这是测试数据库连接,数据库和表结构已创建完成。
### 3. 生成 Prisma Client
```bash
npx prisma generate
```
### 4. 启动开发服务器
```bash
npm run dev
```
服务器将在 http://localhost:3000 启动
## 验证启动
### 1. 健康检查接口
```bash
curl http://localhost:3000/api/v1/health
```
预期响应:
```json
{
"code" : 200 ,
"message" : "success" ,
"data" : {
"status" : "healthy" ,
"timestamp" : "2024-01-15T10:00:00.000Z"
}
}
```
### 2. 测试市场数据接口
```bash
curl http://localhost:3000/api/v1/market/indices
```
## 常见问题
### 问题1: 数据库连接失败
** 错误信息**:
```
Database connection failed: Error: Can't reach database server
```
** 解决方案**:
1. 确认数据库服务器 192.168.0.222:3306 可访问
2. 检查用户名密码是否正确
3. 确认数据库 `aguzhitou` 已创建
** 测试连接**:
```bash
mysql -h 192.168.0.222 -P 3306 -u root -p -e "SHOW DATABASES;"
# 密码: 1qazse42W3
```
### 问题2: Redis 连接失败
** 错误信息**:
```
Redis connection failed
```
** 解决方案**:
- 这是可选依赖,应用会自动降级到内存缓存
- 如需使用 Redis, 请安装并启动 Redis 服务:
```bash
# Docker 方式
docker run -d -p 6379:6379 redis:7-alpine
```
### 问题3: 端口被占用
** 错误信息**:
```
Error: listen EADDRINUSE: address already in use :::3000
```
** 解决方案**:
```bash
# 查看占用进程
lsof -i :3000
# 或修改 .env 中的端口
PORT = 3001
```
### 问题4: Prisma Client 未生成
** 错误信息**:
```
Error: @prisma/client did not initialize yet
```
** 解决方案**:
```bash
npx prisma generate
```
## 目录结构
```
app/backend/
├── src/
│ ├── config/ # 配置文件
│ │ ├── database.ts # 数据库连接
│ │ ├── redis.ts # Redis连接( 带降级)
│ │ └── index.ts # 环境变量配置
│ ├── controllers/ # 控制器
│ ├── services/ # 业务逻辑
│ ├── routes/ # 路由
│ ├── middleware/ # 中间件
│ ├── utils/ # 工具函数
│ ├── websocket/ # WebSocket服务
│ ├── jobs/ # 定时任务
│ └── app.ts # 应用入口
├── prisma/
│ └── schema.prisma # 数据库模型
├── .env # 环境变量(已配置)
└── package.json
```
## API 文档
启动后访问: http://localhost:3000/api/v1/health
详细 API 文档:`app/docs/04-API接口文档.md`
## 生产环境部署
### 1. 构建
```bash
npm run build
```
### 2. 启动生产服务
```bash
npm start
```
### 3. Docker 部署
```bash
docker-compose up -d
```
## 日志查看
日志文件位置:`./logs/`
```bash
# 实时查看日志
tail -f logs/combined-$( date +%Y-%m-%d) .log
# 查看错误日志
tail -f logs/error-$( date +%Y-%m-%d) .log
```
## 联系支持
如有问题,请检查:
1. 数据库连接配置
2. 日志文件中的错误信息
3. 确保所有依赖已正确安装