auto-deploy-demo/.trae/documents/域名配置功能实现方案.md

55 lines
1.7 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.

## 域名配置功能实现方案
### 问题分析
当前系统使用 `localhost` 生成 demo 项目链接,部署到服务器后需要使用指定域名。
### 实现步骤
#### 1. 创建环境配置文件
- 创建 `.env.example` 示例配置文件
- 创建 `.env` 实际配置文件(本地开发用)
- 支持配置项:
- `PORT` - 主服务端口(默认 8888
- `BASE_DOMAIN` - 基础域名(如 `demo.example.com`
- `PROJECT_PORT_START` - 项目端口起始值(默认 9000
- `PROJECT_PORT_END` - 项目端口结束值(默认 9100
#### 2. 安装 dotenv 依赖
- 添加 `dotenv` 包用于读取环境变量
#### 3. 创建配置模块
- 创建 `server/config/index.js` 统一管理配置
- 提供获取项目 URL 的方法,支持域名模式
#### 4. 修改 processManager.js
- 使用配置模块获取域名
- 生成 URL 时根据配置使用域名或 IP:端口 格式
- 支持两种模式:
- **子域名模式**`http://project-id.demo.example.com`
- **端口模式**`http://demo.example.com:9000`
#### 5. 修改 server/index.js
- 启动时加载环境变量配置
#### 6. 前端适配
- 前端 API 请求使用相对路径(已支持)
- 无需额外修改
### 配置示例
```env
# .env 示例
PORT=8888
BASE_DOMAIN=demo.example.com
PROJECT_PORT_START=9000
PROJECT_PORT_END=9100
```
### URL 生成规则
- 本地开发(无 BASE_DOMAIN`http://localhost:9000`
- 服务器部署(有 BASE_DOMAIN`http://demo.example.com:9000`
### 文件修改清单
1. 新建:`.env.example`、`.env`、`server/config/index.js`
2. 修改:`server/index.js`、`server/services/processManager.js`
3. 更新:`package.json`(添加 dotenv 依赖)