Skip to content

update: server startup logic to use next available port#1651

Open
manikanta5827 wants to merge 1 commit intotypicode:mainfrom
manikanta5827:mani-feat/next-available-port
Open

update: server startup logic to use next available port#1651
manikanta5827 wants to merge 1 commit intotypicode:mainfrom
manikanta5827:mani-feat/next-available-port

Conversation

@manikanta5827
Copy link
Contributor

Feature: Auto Port Selection

Fixes #1642

JSON Server now automatically finds the next available port if the specified port is already in use.

Problem

When starting the server with npm run dev or tsx src/bin.ts db.json, if port 3000 is busy, the server would fail to start.

Solution

Added automatic port retry logic that increments the port number until an available port is found.

Example

# If port 3000 is busy, server automatically tries 3001, 3002, etc.
npm run dev
# Output: JSON Server started on PORT :3001

Technical Details

  • Modified startServer() function to retry with port+1 on EADDRINUSE error
  • Updated logRoutes() to use the actual running port instead of original port
@manikanta5827
Copy link
Contributor Author

Screenshot 2025-09-16 at 12 04 15 AM
@manikanta5827
Copy link
Contributor Author

should i display this to user before switching to new port

Port 3000 is in use. Would you like to run the app on another port instead? (Y/n)

CommitToday pushed a commit to CommitToday/json-server that referenced this pull request Nov 9, 2025
Signed-off-by: SoulSniper1212 <warush23@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant