You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

4 years ago
  1. # node-portfinder [![Build Status](https://api.travis-ci.org/http-party/node-portfinder.svg)](https://travis-ci.org/http-party/node-portfinder)
  2. ## Installation
  3. ``` bash
  4. $ [sudo] npm install portfinder
  5. ```
  6. ## Usage
  7. The `portfinder` module has a simple interface:
  8. ``` js
  9. var portfinder = require('portfinder');
  10. portfinder.getPort(function (err, port) {
  11. //
  12. // `port` is guaranteed to be a free port
  13. // in this scope.
  14. //
  15. });
  16. ```
  17. Or with promise (if Promise are supported) :
  18. ``` js
  19. const portfinder = require('portfinder');
  20. portfinder.getPortPromise()
  21. .then((port) => {
  22. //
  23. // `port` is guaranteed to be a free port
  24. // in this scope.
  25. //
  26. })
  27. .catch((err) => {
  28. //
  29. // Could not get a free port, `err` contains the reason.
  30. //
  31. });
  32. ```
  33. If `portfinder.getPortPromise()` is called on a Node version without Promise (<4), it will throw an Error unless [Bluebird](http://bluebirdjs.com/docs/getting-started.html) or any Promise pollyfill is used.
  34. ### Ports search scope
  35. By default `portfinder` will start searching from `8000` and scan until maximum port number (`65535`) is reached.
  36. You can change this globally by setting:
  37. ```js
  38. portfinder.basePort = 3000; // default: 8000
  39. portfinder.highestPort = 3333; // default: 65535
  40. ```
  41. or by passing optional options object on each invocation:
  42. ```js
  43. portfinder.getPort({
  44. port: 3000, // minimum port
  45. stopPort: 3333 // maximum port
  46. }, callback);
  47. ```
  48. ## Run Tests
  49. ``` bash
  50. $ npm test
  51. ```
  52. #### Author: [Charlie Robbins][0]
  53. #### Maintainer: [Erik Trom][1]
  54. #### License: MIT/X11
  55. [0]: http://nodejitsu.com
  56. [1]: https://github.com/eriktrom