From 9d4581a2b59da512cf3c50b1967d3a3fab607a2c Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Fri, 19 Sep 2025 16:17:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=AD=A3=E5=B8=B8=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E5=90=8E=E7=AB=AFapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/api.js | 4 ++-- src/utils/http.js | 3 ++- webpack.config.js | 9 ++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/config/api.js b/src/config/api.js index cc6991b2..3f1cd975 100644 --- a/src/config/api.js +++ b/src/config/api.js @@ -1,11 +1,11 @@ // API配置文件 const API_CONFIG = { // 后端服务基础URL - BASE_URL: 'http://localhost:8086', + BASE_URL: '', // API端点 ENDPOINTS: { - DEVICES: '/api/v1/devices' + DEVICES: '/v1/devices' }, // 请求超时时间(毫秒) diff --git a/src/utils/http.js b/src/utils/http.js index 8c00a895..4864bbf5 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,7 +1,8 @@ import axios from 'axios'; import API_CONFIG from '../config/api.js'; -// 创建axios实例 +// 创建axios实例,自动将请求发送到 BASE_URL + 相对路径 +// 例如:http.get('/api/pigs') 会请求 BASE_URL + '/api/pigs' const http = axios.create({ baseURL: API_CONFIG.BASE_URL, timeout: API_CONFIG.TIMEOUT, diff --git a/webpack.config.js b/webpack.config.js index 6a413177..895f5ed9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -46,7 +46,14 @@ module.exports = (env, argv) => { hot: true, open: true, historyApiFallback: true, - port: 8080 + port: 8080, + proxy: { + '/api': { + target: 'http://localhost:8086', + changeOrigin: true, + secure: false + } + } }, resolve: { alias: {