|
| 1 | +# 部署说明 |
| 2 | + |
| 3 | +## 开发环境启动 |
| 4 | + |
| 5 | +### 前提条件 |
| 6 | +1. 确保已安装 Node.js 16 或更高版本 |
| 7 | +2. 确保后端服务已在 `http://localhost:8123` 运行 |
| 8 | + |
| 9 | +### Windows 用户 |
| 10 | +双击运行 `start.bat` 文件,或在命令行中执行: |
| 11 | +```cmd |
| 12 | +start.bat |
| 13 | +``` |
| 14 | + |
| 15 | +### macOS/Linux 用户 |
| 16 | +在终端中执行: |
| 17 | +```bash |
| 18 | +./start.sh |
| 19 | +``` |
| 20 | + |
| 21 | +### 手动启动 |
| 22 | +1. 安装依赖: |
| 23 | +```bash |
| 24 | +npm install |
| 25 | +``` |
| 26 | + |
| 27 | +2. 启动开发服务器: |
| 28 | +```bash |
| 29 | +npm run dev |
| 30 | +``` |
| 31 | + |
| 32 | +访问地址:`http://localhost:3000` |
| 33 | + |
| 34 | +## 生产环境部署 |
| 35 | + |
| 36 | +### 1. 构建项目 |
| 37 | +```bash |
| 38 | +npm run build |
| 39 | +``` |
| 40 | +构建完成后,`dist` 目录包含所有静态文件。 |
| 41 | + |
| 42 | +### 2. 部署到服务器 |
| 43 | + |
| 44 | +#### 使用 Nginx |
| 45 | +```nginx |
| 46 | +server { |
| 47 | + listen 80; |
| 48 | + server_name your-domain.com; |
| 49 | + root /path/to/dist; |
| 50 | + index index.html; |
| 51 | +
|
| 52 | + # 处理 Vue Router 的历史模式 |
| 53 | + location / { |
| 54 | + try_files $uri $uri/ /index.html; |
| 55 | + } |
| 56 | +
|
| 57 | + # API 代理到后端 |
| 58 | + location /api { |
| 59 | + proxy_pass http://localhost:8123; |
| 60 | + proxy_set_header Host $host; |
| 61 | + proxy_set_header X-Real-IP $remote_addr; |
| 62 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 63 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +#### 使用 Apache |
| 69 | +在 `dist` 目录创建 `.htaccess` 文件: |
| 70 | +```apache |
| 71 | +<IfModule mod_rewrite.c> |
| 72 | + RewriteEngine On |
| 73 | + RewriteBase / |
| 74 | + RewriteRule ^index\.html$ - [L] |
| 75 | + RewriteCond %{REQUEST_FILENAME} !-f |
| 76 | + RewriteCond %{REQUEST_FILENAME} !-d |
| 77 | + RewriteRule . /index.html [L] |
| 78 | +</IfModule> |
| 79 | +``` |
| 80 | + |
| 81 | +### 3. 环境变量配置 |
| 82 | +在生产环境中,可以创建 `.env.production` 文件配置环境变量: |
| 83 | +``` |
| 84 | +VITE_API_BASE_URL=https://your-api-domain.com/api |
| 85 | +``` |
| 86 | + |
| 87 | +## Docker 部署 |
| 88 | + |
| 89 | +### 1. 创建 Dockerfile |
| 90 | +```dockerfile |
| 91 | +# 构建阶段 |
| 92 | +FROM node:18-alpine as build-stage |
| 93 | +WORKDIR /app |
| 94 | +COPY package*.json ./ |
| 95 | +RUN npm install |
| 96 | +COPY . . |
| 97 | +RUN npm run build |
| 98 | + |
| 99 | +# 生产阶段 |
| 100 | +FROM nginx:alpine as production-stage |
| 101 | +COPY --from=build-stage /app/dist /usr/share/nginx/html |
| 102 | +COPY nginx.conf /etc/nginx/conf.d/default.conf |
| 103 | +EXPOSE 80 |
| 104 | +CMD ["nginx", "-g", "daemon off;"] |
| 105 | +``` |
| 106 | + |
| 107 | +### 2. 创建 nginx.conf |
| 108 | +```nginx |
| 109 | +server { |
| 110 | + listen 80; |
| 111 | + server_name localhost; |
| 112 | + root /usr/share/nginx/html; |
| 113 | + index index.html; |
| 114 | +
|
| 115 | + location / { |
| 116 | + try_files $uri $uri/ /index.html; |
| 117 | + } |
| 118 | +
|
| 119 | + location /api { |
| 120 | + proxy_pass http://backend:8123; |
| 121 | + proxy_set_header Host $host; |
| 122 | + proxy_set_header X-Real-IP $remote_addr; |
| 123 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 124 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### 3. 构建和运行 |
| 130 | +```bash |
| 131 | +docker build -t coder-test-frontend . |
| 132 | +docker run -p 80:80 coder-test-frontend |
| 133 | +``` |
| 134 | + |
| 135 | +## 常见问题 |
| 136 | + |
| 137 | +### 1. 跨域问题 |
| 138 | +如果遇到跨域问题,确保: |
| 139 | +- 开发环境中 Vite 代理配置正确 |
| 140 | +- 生产环境中 Nginx/Apache 代理配置正确 |
| 141 | +- 后端服务允许跨域请求 |
| 142 | + |
| 143 | +### 2. 路由问题 |
| 144 | +如果页面刷新后出现 404: |
| 145 | +- 确保服务器配置了 Vue Router 的历史模式支持 |
| 146 | +- 检查 `try_files` 配置是否正确 |
| 147 | + |
| 148 | +### 3. API 请求失败 |
| 149 | +检查: |
| 150 | +- 后端服务是否正常运行 |
| 151 | +- API 地址是否正确 |
| 152 | +- 网络连接是否正常 |
| 153 | + |
| 154 | +### 4. 构建失败 |
| 155 | +常见原因: |
| 156 | +- Node.js 版本过低 |
| 157 | +- 依赖包版本冲突 |
| 158 | +- 内存不足 |
| 159 | + |
| 160 | +解决方案: |
| 161 | +```bash |
| 162 | +# 清除缓存 |
| 163 | +npm cache clean --force |
| 164 | + |
| 165 | +# 删除 node_modules 重新安装 |
| 166 | +rm -rf node_modules package-lock.json |
| 167 | +npm install |
| 168 | +``` |
0 commit comments