Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

实用的端口检测模块detect-port

$
0
0

特点

  • 可检测socket端口
  • 无其他模块依赖
  • 较全面的单测覆盖
  • npm下载量、使用量高
  • 集成了命令行工具

相关链接

github: https://github.com/xudafeng/detect-port npm: https://www.npmjs.com/package/detect-port


支持多种异步使用方式

Usage

$ npm i detect-port --save
const detect = require('detect-port');

/**
 * callback usage
 */

detect(port, (err, _port) => {
  if (err) {
    console.log(err);
  }

  if (port === _port) {
    console.log(`port: ${port} was not occupied`);
  } else {
    console.log(`port: ${port} was occupied, try port: ${_port}`);
  }
});

/**
 * for a yield syntax instead of callback function implement
 */

const co = require('co');

co(function *() {
  const _port = yield detect(port);

  if (port === _port) {
    console.log(`port: ${port} was not occupied`);
  } else {
    console.log(`port: ${port} was occupied, try port: ${_port}`);
  }
});

/**
 * use as a promise
 */

detect(port)
  .then(_port => {
    if (port === _port) {
      console.log(`port: ${port} was not occupied`);
    } else {
      console.log(`port: ${port} was occupied, try port: ${_port}`);
    }
  })
  .catch(err => {
    console.log(err);
  });

Command Line Tool

$ npm i detect-port -g

Quick Start

# get an available port randomly
$ detect

# detect pointed port
$ detect 80

# more help
$ detect --help
```![1.png](//dn-cnode.qbox.me/FsPjKSqrTdfPKto5IWjL2Ez8DMjs)

Viewing all articles
Browse latest Browse all 14821

Trending Articles