update: server startup logic to use next available port #1651
+40
−21
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.
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
ortsx 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
Technical Details
startServer()
function to retry withport+1
onEADDRINUSE
errorlogRoutes()
to use the actual running port instead of original port