Files
Nexus_Edu/backend/README.md
2025-11-28 19:23:19 +08:00

138 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# EduNexus Backend API
后端API服务基于Express + Prisma + MySQL。
## 快速开始
### 1. 安装依赖
```bash
cd backend
npm install
```
### 2. 配置环境变量
复制`.env.example``.env`并配置数据库连接:
```bash
cp .env.example .env
```
编辑`.env`
```env
DATABASE_URL="mysql://用户名:密码@localhost:3306/edunexus"
```
### 3. 初始化数据库
```bash
# 生成Prisma客户端
npm run prisma:generate
# 同步数据库schema
npm run prisma:push
```
### 4. (可选) 导入示例数据
在MySQL中执行
```bash
mysql -u root -p edunexus < ../database/seed.sql
```
### 5. 启动服务器
```bash
# 开发模式
npm run dev
# 生产模式
npm run build
npm start
```
服务器将在 http://localhost:3001 启动
## API端点
### Auth `/api/auth`
- `POST /register` - 注册
- `POST /login` - 登录
- `GET /profile` - 获取个人信息 🔒
- `PUT /profile` - 更新个人信息 🔒
- `POST /change-password` - 修改密码 🔒
### Exams `/api/exams`
- `GET /` - 获取试卷列表 🔒
- `GET /:id` - 获取试卷详情 🔒
- `POST /` - 创建试卷 🔒
- `PUT /:id` - 更新试卷 🔒
- `DELETE /:id` - 删除试卷 🔒
- `POST /:id/nodes` - 添加节点 🔒
- `PUT /:id/nodes/:nodeId` - 更新节点 🔒
- `DELETE /:id/nodes/:nodeId` - 删除节点 🔒
### 其他模块
- `/api/org` - 组织架构 (TODO)
- `/api/curriculum` - 教材知识 (TODO)
- `/api/questions` - 题库 (TODO)
- `/api/assignments` - 作业 (TODO)
- `/api/submissions` - 提交 (TODO)
- `/api/grading` - 批阅 (TODO)
> 🔒 = 需要JWT认证
## 认证
API使用JWT Bearer Token认证。
1. 调用`/api/auth/login`获取token
2. 在请求头中添加:`Authorization: Bearer <token>`
## 项目结构
```
backend/
├── src/
│ ├── controllers/ # 控制器
│ ├── routes/ # 路由
│ ├── middleware/ # 中间件
│ ├── utils/ # 工具函数
│ └── index.ts # 入口文件
├── prisma/
│ └── schema.prisma # Prisma模型定义
└── package.json
```
## 数据库
使用Prisma ORM管理MySQL数据库
```bash
# 查看数据库
npm run prisma:studio
# 创建迁移
npm run prisma:migrate
# 重置数据库
npm run prisma:push
```
## 开发指南
- 所有API返回JSON格式
- 使用UUID作为主键
- 支持软删除 (isDeleted字段)
- 所有表包含审计字段
- ExamNode支持无限层级嵌套
## 待完成
- [ ] 完善其他6个模块的API实现
- [ ] 添加数据验证中间件
- [ ] 实现文件上传功能
- [ ] 添加单元测试
- [ ] 添加API文档Swagger