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

@ -13,4 +13,10 @@ ZEP_API_KEY=your_zep_api_key_here
# 注意如果不使用加速配置env文件中就不要出现下面的配置项
LLM_BOOST_API_KEY=your_api_key_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,24 +1,29 @@
import { defineConfig } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
// https://vite.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'@locales': path.resolve(__dirname, '../locales')
}
},
server: {
port: 3000,
open: true,
proxy: {
'/api': {
target: 'http://localhost:5001',
changeOrigin: true,
secure: false
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, path.resolve(__dirname, '..'), '')
const backendUrl = env.VITE_API_BASE_URL || 'http://localhost:5001'
return {
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'@locales': path.resolve(__dirname, '../locales')
}
},
server: {
port: 3000,
open: true,
proxy: {
'/api': {
target: backendUrl,
changeOrigin: true,
secure: false
}
}
}
}