nstallation
$ [sudo] npm install portfinder
Usage
var portfinder = require('portfinder'); portfinder.getPort(function (err, port) {
//
// `port` is guaranteed to be a free port
// in this scope.
//
});
Promise形式
const portfinder = require('portfinder'); portfinder.getPortPromise()
.then((port) => {
//
// `port` is guaranteed to be a free port
// in this scope.
//
})
.catch((err) => {
//
// Could not get a free port, `err` contains the reason.
//
});
Ports search scope
By default portfinder
will start searching from 8000
and scan until maximum port number (65535
) is reached.
You can change this globally by setting:
portfinder.basePort = 3000; // default: 8000
portfinder.highestPort = 3333; // default: 65535
默认端口从8000扫描到65535,上面两个方法可以i实现自定义扫描范围
or by passing optional options object on each invocation:
portfinder.getPort({
port: 3000, // minimum port
stopPort: 3333 // maximum port
}, callback);