# EChat 后端 **Repository Path**: zhaoStore/echat-nodejs-server ## Basic Information - **Project Name**: EChat 后端 - **Description**: 学习Electron的练手项目后端,基于nodejs实现的简单后端 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-30 - **Last Updated**: 2025-05-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 📖 Chat 后端接口文档 ## 📦 通用说明 - 地址:`http://localhost:3000` - 数据格式:`application/json` - 文件上传:`multipart/form-data` - 鉴权:暂未启用(如需可扩展 JWT) - !!! 有些接口文档未更新或未添加 !!! ------ ## 📁 用户接口 ### 🧾 用户注册 - **接口地址**:`POST /api/auth/register` - **请求参数**: ``` json复制编辑{ "username": "test123", "password": "123456" } ``` - **返回示例**: ``` json复制编辑{ "message": "注册成功", "userId": "662156bc8b712ff0c159f101" } ``` ------ ### 🔑 用户登录 - **接口地址**:`POST /api/auth/login` - **请求参数**: ``` json复制编辑{ "username": "test123", "password": "123456" } ``` - **返回示例**: ``` json复制编辑{ "message": "登录成功", "userId": "662156bc8b712ff0c159f101" } ``` ------ ## 🔍 查找用户 - **接口地址**:`GET /api/user/find` 或 `POST /api/user/find` - **请求参数**: ``` { "username": "test123" } ``` - **返回示例**: ``` { "_id": "662156bc8b712ff0c159f101", "username": "test123", "password": "123456", "friends": [] } ``` ## 👥 好友系统 ### 📌 添加好友 - **接口地址**:`Socket.io 事件` - **事件名**:`add_friend` - **请求参数**: ``` json复制编辑{ "userId": "发送者ID", "friendId": "要添加的好友ID" } ``` - **返回结果**:无返回,双方数据库添加成功后,通过数据库更新获取好友列表。 ------ ## 💬 聊天系统 ### 💬 一对一聊天(私聊) 注:接口有更新,文档暂未更新 - **接口方式**:`Socket.io` - **事件名**:`private_message` - **请求参数**: ``` json复制编辑{ "senderId": "发送者ID", "receiverId": "接收者ID", "type": "text | image", "content": "消息内容(文字或图片URL)" } ``` - **返回事件**:`private_message` ``` json复制编辑{ "_id": "消息ID", "sender": "发送者ID", "receiver": "接收者ID", "type": "text", "content": "你好", "timestamp": "2025-04-09T00:00:00Z" } ``` ------ ### 💬 群聊发送消息 注:暂未测试/实现 - **接口方式**:`Socket.io` - **事件名**:`group_message` - **请求参数**: ``` json复制编辑{ "senderId": "发送者ID", "groupId": "群聊ID", "type": "text | image", "content": "内容" } ``` - **返回事件**:`group_message` ``` json复制编辑{ "_id": "消息ID", "sender": "发送者ID", "groupId": "群聊ID", "type": "text", "content": "大家好", "timestamp": "2025-04-09T00:00:00Z" } ``` ------ ### 🔗 加入群聊 注:暂未测试/实现 - **接口方式**:`Socket.io` - **事件名**:`join_group` - **请求参数**: ``` json复制编辑{ "groupId": "群聊ID" } ``` - **返回**:无 ------ ### 👤 注册 socket 身份(用于绑定私聊房间) 注:暂未测试/实现 - **事件名**:`register` - **请求参数**: ``` json复制编辑{ "userId": "用户ID" } ``` ------ ## 🖼 图片上传 ### 📤 上传图片 - **接口地址**:`POST /api/upload` - **请求类型**:`multipart/form-data` - **参数说明**: | 参数名 | 类型 | 是否必填 | 描述 | | ------ | ---- | -------- | ------------------------------- | | image | File | 是 | 图片文件(jpg/png/webp/gif 等) | - **返回示例**: ``` json复制编辑{ "url": "/uploads/1683738798123.jpg" } ``` - 使用方法:上传后将返回的 `url` 填入消息 `content`,并设置 `type: "image"` 发送聊天 ------ ## 📁 静态资源访问 - 图片静态地址示例:`http://localhost:3000/uploads/1683738798123.jpg`