fix: read VITE_API_BASE_URL from .env for Vite dev proxy (fixes #480)

The Vite development proxy target was hardcoded to http://localhost:5001,
ignoring the VITE_API_BASE_URL environment variable that the axios client
already respects. This caused the dev proxy to still route to localhost even
when users configured a remote or custom backend address.

- Use loadEnv() in vite.config.js to read VITE_API_BASE_URL from the project
  root .env file and pass it as the proxy target (fallback: localhost:5001)
- Document VITE_API_BASE_URL in .env.example so users know the variable exists
This commit is contained in:
Octopus 2026-04-07 10:30:35 +08:00
parent fa0f6519b1
commit 9961d59c78
2 changed files with 29 additions and 18 deletions

View File

@ -14,3 +14,9 @@ ZEP_API_KEY=your_zep_api_key_here
LLM_BOOST_API_KEY=your_api_key_here LLM_BOOST_API_KEY=your_api_key_here
LLM_BOOST_BASE_URL=your_base_url_here LLM_BOOST_BASE_URL=your_base_url_here
LLM_BOOST_MODEL_NAME=your_model_name_here LLM_BOOST_MODEL_NAME=your_model_name_here
# ===== 前端 / 部署配置(可选)=====
# 后端 API 地址axios 客户端 + Vite 开发代理均读取此变量)
# 默认值http://localhost:5001
# 若后端运行在其他主机或端口,请取消注释并修改为对应地址
# VITE_API_BASE_URL=http://localhost:5001

View File

@ -1,9 +1,13 @@
import { defineConfig } from 'vite' import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import path from 'path' import path from 'path'
// https://vite.dev/config/ // https://vite.dev/config/
export default defineConfig({ export default defineConfig(({ mode }) => {
const env = loadEnv(mode, path.resolve(__dirname, '..'), '')
const backendUrl = env.VITE_API_BASE_URL || 'http://localhost:5001'
return {
plugins: [vue()], plugins: [vue()],
resolve: { resolve: {
alias: { alias: {
@ -16,10 +20,11 @@ export default defineConfig({
open: true, open: true,
proxy: { proxy: {
'/api': { '/api': {
target: 'http://localhost:5001', target: backendUrl,
changeOrigin: true, changeOrigin: true,
secure: false secure: false
} }
} }
} }
}
}) })