Skip to content

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Find available ports starting from 3000

1 participant