node-portfinder

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);

 

喜欢这篇文章?欢迎打赏~~

 

posted @ 2019-04-25 18:52  苍青浪  阅读(917)  评论(0编辑  收藏  举报