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