detect-port
Node.js implementation of port detector
Last updated 7 years ago by fengmk2 .
MIT · Repository · Bugs · Original npm
$ cnpm install detect-port 
SYNC missed versions from official npm registry.

logo


NPM version build status Test coverage npm download

Node.js implementation of port detector

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

# output verbose log
$ detect --verbose

# more help
$ detect --help

FAQ

Most likely network error, check that your /etc/hosts and make sure the content below:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

Contributors


xudafeng


fengmk2


ziczhu


gaearon


popomore


jsw0528

This project follows the git-contributor spec, auto upated at Tue Aug 14 2018 12:43:52 GMT+0800.

License

MIT

Current Tags

  • 1.5.1                                ...           latest (3 years ago)

4 Versions

  • 1.5.1                                ...           3 years ago
  • 1.2.3                                ...           7 years ago
  • 1.3.0                                ...           7 years ago
  • 1.0.1                                ...           9 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (8)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org