feat(dev): extract ports to env config and optimize startup order#25
Open
mmletgo wants to merge 1 commit into
Open
feat(dev): extract ports to env config and optimize startup order#25mmletgo wants to merge 1 commit into
mmletgo wants to merge 1 commit into
Conversation
- Move hardcoded ports (3000/5173) to env vars (SERVER_PORT/VITE_DEV_PORT) - Add dev:after-server script to start shared+client only after server is ready - Increase wait-for-port timeout from 120s to 300s for slow cold starts - Avoid ts-node-dev and shared tsc watch competing for resources concurrently Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts the development-time port and startup orchestration so the backend comes up first, then the shared package and frontend start after the server is reachable, while also moving toward env-configurable ports.
Changes:
- Switch server default port to 3001 and update client defaults accordingly.
- Add
wait-for-portsupport for env-driven server port and extend its timeout. - Restructure
pnpm devstartup to start server first, then start shared + client after readiness.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
server/src/app.ts |
Updates the server’s default port fallback to 3001. |
scripts/wait-for-port.cjs |
Uses env-driven default port and increases timeout for slow cold starts. |
package.json |
Reorders dev startup and adds a post-server step for shared+client. |
client/vite.config.ts |
Makes the Vite dev server port configurable via env. |
client/src/lib/constants.ts |
Allows API port to be driven by VITE_API_PORT with a new 3001 default. |
client/.env.example |
Updates documented frontend env overrides for dev. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
12
to
16
| const configuredBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim(); | ||
| const apiPort = import.meta.env.VITE_API_PORT?.trim() || "3001"; | ||
| if (!import.meta.env.DEV || typeof window === "undefined") { | ||
| return configuredBaseUrl || "http://localhost:3000/api"; | ||
| return configuredBaseUrl || `http://localhost:${apiPort}/api`; | ||
| } |
| parsed.hostname = window.location.hostname; | ||
| if (!parsed.port) { | ||
| parsed.port = "3000"; | ||
| parsed.port = import.meta.env.VITE_API_PORT?.trim() || "3001"; |
Comment on lines
+2
to
+3
| # API 服务地址(包含端口),默认 http://localhost:3001/api | ||
| VITE_API_BASE_URL=http://localhost:3001/api |
| : [...DEFAULT_HOSTS], | ||
| port: 3000, | ||
| timeoutMs: 120000, | ||
| port: Number(process.env.SERVER_PORT) || 3001, |
|
|
||
| function printHelp() { | ||
| console.log("Usage: node scripts/wait-for-port.cjs [--host 127.0.0.1,localhost,::1] [--port 3000] [--timeout 120000] [--interval 500]"); | ||
| console.log("Usage: node scripts/wait-for-port.cjs [--host 127.0.0.1,localhost,::1] [--port 3001] [--timeout 300000] [--interval 500]"); |
| "dev:server:log": "node scripts/run-with-log.cjs --name server -- pnpm --filter @ai-novel/server dev", | ||
| "dev:client": "pnpm --filter @ai-novel/client dev", | ||
| "dev:client:wait": "node scripts/wait-for-port.cjs --port 3000 && pnpm dev:client", | ||
| "dev:client:wait": "node scripts/wait-for-port.cjs --port 3001 && pnpm dev:client", |
Comment on lines
65
to
68
| server: { | ||
| host: true, // 允许局域网访问(监听 0.0.0.0) | ||
| port: Number(process.env.VITE_DEV_PORT) || 5174, | ||
| host: true, | ||
| }, |
Comment on lines
+2
to
7
| # API 服务地址(包含端口),默认 http://localhost:3001/api | ||
| VITE_API_BASE_URL=http://localhost:3001/api | ||
| # Vite 开发服务器端口 | ||
| VITE_DEV_PORT=5174 | ||
| # 默认 10 分钟,可按需要调大。 | ||
| VITE_API_TIMEOUT_MS=600000 |
Comment on lines
161
to
165
| const ragCompatibilityReport = await initializeRagSettingsCompatibility(); | ||
| const app = createApp(); | ||
| const port = Number(process.env.PORT ?? 3000); | ||
| const port = Number(process.env.PORT ?? 3001); | ||
| const allowLan = parseEnvFlag(process.env.ALLOW_LAN, process.env.NODE_ENV !== "production"); | ||
| const host = process.env.HOST ?? (allowLan ? "0.0.0.0" : "localhost"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SERVER_PORT,VITE_DEV_PORT,VITE_API_PORT) for easy configuration and conflict avoidancewait-for-porttimeout from 120s to 300s to accommodate slow first-time compilationChanges
server/src/app.tsclient/vite.config.tsVITE_DEV_PORTenvclient/src/lib/constants.tsVITE_API_PORTenvclient/.env.exampleVITE_API_BASE_URLandVITE_DEV_PORTscripts/wait-for-port.cjsSERVER_PORTenv, increase timeout to 300spackage.jsondev:after-serverscript, restructuredev:rawstartup orderTest plan
pnpm devstarts server first, shared + client start after server is ready🤖 Generated with Claude Code