From f1242103b2583898e5d4f0d5235ef76209a5f56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E9=98=B3?= <2260837959@qq.com> Date: Tue, 2 Dec 2025 08:54:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E6=84=8F=E7=AB=AF=E5=8F=A3=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ config/proxy.ts | 9 +++++++-- package.json | 12 +++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2f9c625c..72635582 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,13 @@ $ cd myapp && tyarn // or $ cd myapp && npm install ``` +# 任意端口启动 +$ PORT=8003 npm run start +$ PORT=3000 npm run start +$ PORT=8080 npm run start +# 使用默认端口8001 +npm run start ## Browsers support Modern browsers. diff --git a/config/proxy.ts b/config/proxy.ts index aaae8f07..e594fcb4 100644 --- a/config/proxy.ts +++ b/config/proxy.ts @@ -9,13 +9,18 @@ * * @doc https://umijs.org/docs/guides/proxy */ +// 获取当前端口,默认8001 +const getCurrentPort = () => { + return process.env.PORT || '8001'; +}; + export default { // 如果需要自定义本地开发服务器 请取消注释按需调整 dev: { - // localhost:8003/api/** -> http://localhost:8003/api/ + // localhost:8001/api/** -> http://localhost:8001/api/ '/api/': { // 要代理的地址 - 指向开发服务器自身 - target: 'http://localhost:8003', + target: `http://localhost:${getCurrentPort()}`, // 配置了这个可以从 http 代理到 https // 依赖 origin 的功能可能需要这个,比如 cookie changeOrigin: true, diff --git a/package.json b/package.json index ec6e5fcc..b38552b1 100644 --- a/package.json +++ b/package.json @@ -21,11 +21,13 @@ "preview": "npm run build && max preview --port 8000", "record": "cross-env NODE_ENV=development UMI_ENV=test max record --scene=login", "serve": "umi-serve", - "start": "cross-env UMI_ENV=dev PORT=8003 max dev", - "start:dev": "cross-env UMI_ENV=dev MOCK=none PORT=8003 max dev", - "start:no-mock": "cross-env MOCK=none PORT=8003 max dev", - "start:pre": "cross-env UMI_ENV=pre MOCK=none PORT=8003 max dev", - "start:test": "cross-env UMI_ENV=test MOCK=none PORT=8003 max dev", + "start": "cross-env UMI_ENV=dev PORT=${PORT:-8001} max dev", + "start:dev": "cross-env UMI_ENV=dev MOCK=none max dev", + "start:no-mock": "cross-env MOCK=none max dev", + "start:pre": "cross-env UMI_ENV=pre MOCK=none max dev", + "start:test": "cross-env UMI_ENV=test MOCK=none max dev", + "start:port": "cross-env UMI_ENV=dev PORT=%npm_config_port% max dev", + "start:8005": "cross-env UMI_ENV=dev PORT=8005 max dev", "test": "jest", "test:coverage": "npm run jest -- --coverage", "test:update": "npm run jest -- -u",