npm install -g pnpm
git clone <repository-url>
cd content-frontend
pnpm install
cp .env.example .env.development
编辑 .env.development:
# 修改为你的后端地址
VITE_API_BASE_URL=http://localhost:8080
pnpm dev
项目将在 http://localhost:3000 启动。
开发环境下,所有 API 请求会通过 Vite 代理转发到后端:
前端: http://localhost:3000
后端: http://localhost:8080
请求流程:
http://localhost:3000/sys/user/list
↓ (Vite 代理)
http://localhost:8080/sys/user/list
在 vite.config.ts 中配置:
proxy: {
"/api": {
target: "http://localhost:8080",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
"/sys": {
target: "http://localhost:8080",
changeOrigin: true,
},
}
打开浏览器控制台,执行:
fetch('/sys/user/list')
.then(r => r.json())
.then(console.log)
访问示例页面测试 API:
http://localhost:3000/examples/api-usage
错误信息:
Port 3000 is in use
解决方案:
修改 vite.config.ts 中的端口:
server: {
port: 3001, // 改为其他端口
}
原因: 请求路径不匹配代理规则
解决方案:
确保 API 请求路径以 /api 或 /sys 开头:
// ✅ 正确
http.get("/sys/user/list");
// ❌ 错误
http.get("http://localhost:8080/sys/user/list");
原因: 后端未配置 CORS
解决方案:
原因: Cookie 配置问题
解决方案:
在代理配置中添加:
"/api": {
target: "http://localhost:8080",
changeOrigin: true,
cookieDomainRewrite: "localhost",
}
pnpm build
构建产物在 dist 目录。
pnpm preview
编辑 .env.production:
# 生产环境后端地址
VITE_API_BASE_URL=https://api.example.com
将 dist 目录部署到任意静态文件服务器(Nginx、Apache 等)。
FROM nginx:alpine
COPY dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.html;
# 前端路由
location / {
try_files $uri $uri/ /index.html;
}
# API 代理
location /api {
proxy_pass http://backend:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /sys {
proxy_pass http://backend:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
src/
├── layouts/ # 布局组件
├── pages/ # 页面组件
├── components/ # 通用组件
├── services/ # API 服务
├── hooks/ # 自定义 Hooks
├── contexts/ # React Context
├── utils/ # 工具函数
└── types/ # 类型定义
feat: 新功能
fix: 修复 bug
docs: 文档更新
style: 代码格式调整
refactor: 重构
test: 测试相关
chore: 构建/工具相关
src/examples/