Skip to content

replace portfinder with newport #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

replace portfinder with newport #332

wants to merge 1 commit into from

Conversation

harttle
Copy link

@harttle harttle commented Dec 6, 2016

Fixes: #325 and #215

@derhuerst
Copy link

By using newport, http-server loses the ability to specify a specific port (which is VERY handy imo) and fallback to the next available. Also, the defacto standard port for adhoc/dev HTTP servers is 8080.

@harttle
Copy link
Author

harttle commented Dec 6, 2016

I think it's faster and also valid to let Node.js server to use a random clean port :)

@derhuerst
Copy link

Still, this is a regression. As some people use this for serving static files permanently, http-server will effectively be broken for them.

@xuexb
Copy link

xuexb commented Dec 23, 2016

This is one of my ideas : Default port is 8080, if the error is random.

If set up over the port, when being given an error I think it is reasonable. If no port is set, the next port is randomly enabled when the 8080 is occupied for the service to start

process.on('uncaughtException', function (err) {
  if (err.syscall === 'listen' && err.port === portfinder.basePort) {
    console.log(colors.red(portfinder.basePort + ' port is occupied, will randomly generate a new port!\n\n'));
    newport(function (err, port) {
      if (err) { throw err; }
      listen(port);
    });
  }
});

if (!port) {
  portfinder.basePort = 8080;
  portfinder.getPort(function (err, port) {
    if (err) { throw err; }
    listen(port);
  });
}
else {
  listen(port);
}

image

Thanks~

@derhuerst
Copy link

derhuerst commented Dec 23, 2016

This is one of my ideas : Default port is 8080, if the error is random.

If set up over the port, when being given an error I think it is reasonable. If no port is set, the next port is randomly enabled when the 8080 is occupied for the service to start

I'd personally rather like the opposite: I would like to specify a port (and have it fail if the port is used). In case I didn't care about a specific port and just want any port, I would pass --find-port, which uses newport.

This would work out using only newport, without catching via uncaughtException, and without being a regression over the previous behavior.

Af few notes on your implementation:

  • Using process.on('uncaughtException', cb) is considered dangerous & bad practice. Additionally, process.once would be more appropriate.
  • console.log(colors.red( should be replaced by something that writes to stderr (instead of stdout) and sets the exit code to non-0.
  • Your code uses portfinder & newport. Why not use one of them?

@xuexb
Copy link

xuexb commented Dec 24, 2016

Above is just one of my ideas, really lack of thinking~ Thank you very much~

I'd personally rather like the opposite: I would like to specify a port (and have it fail if the port is used). In case I didn't care about a specific port and just want any port, I would pass --find-port, which uses newport.

This is good~

@BigBlueHat
Copy link
Member

I'm assisting @indexzero in catching up with the backlog of PRs here.

@derhuerst I'm 👍 on the approach you've outlined, and would be happy to accept a PR.

@harttle would you be in support of the approach outlined in #332 (comment)

I'm not keen to loose the ability to define a specific port, but having the option to fallback / have one found for me, sounds fab!

@harttle
Copy link
Author

harttle commented Apr 26, 2017

@BigBlueHat OK, I'll make a PR this week later.

@harttle
Copy link
Author

harttle commented Apr 26, 2017

@BigBlueHat I've implemented the --find-port option. Please ping this thread if there's still anything I can help.

@BigBlueHat
Copy link
Member

@harttle just took this for a spin locally (bash in Windows 10) and it worked like a charm!

I'm going to give this a bit for others to test, then possibly add it to the v0.10.0 release.

Much thanks!
🎩

@harttle
Copy link
Author

harttle commented Apr 27, 2017

I just find out that upgrade the portfinder fixes this issue without any regression. The last commit would be better.

@BigBlueHat
Copy link
Member

@harttle oh. Even better. Mind squashing this PR into just the necessary code?

@harttle
Copy link
Author

harttle commented Apr 28, 2017

It's Ok, there is only one commit actually, since I reset it before.☺️

@BigBlueHat
Copy link
Member

@harttle looks like this is just the portfinder update, correct? If that's the case, that means this code doesn't currently provide the --find-port option. Would you mind adding that to this PR?

Thanks!

@BigBlueHat
Copy link
Member

@harttle I'm going to close this one--as it's a bit tangled now--in favor of the newly opened #363 which is specifically about the --find-port param.

I've upgraded portfinder in 989fa1c by way of keeping things up to date.

Thanks for taking the lead on this feature!
🎩

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.

4 participants