| 1 | [![logo][logo-image]][logo-url]
|
|---|
| 2 |
|
|---|
| 3 | ---
|
|---|
| 4 |
|
|---|
| 5 | [![NPM version][npm-image]][npm-url]
|
|---|
| 6 | [![build status][travis-image]][travis-url]
|
|---|
| 7 | [![Test coverage][codecov-image]][codecov-url]
|
|---|
| 8 | [![npm download][download-image]][download-url]
|
|---|
| 9 |
|
|---|
| 10 | [logo-image]: ./logo.png
|
|---|
| 11 | [logo-url]: https://npmjs.org/package/detect-port
|
|---|
| 12 | [npm-image]: https://img.shields.io/npm/v/detect-port.svg?style=flat-square
|
|---|
| 13 | [npm-url]: https://npmjs.org/package/detect-port
|
|---|
| 14 | [travis-image]: https://img.shields.io/travis/node-modules/detect-port.svg?style=flat-square
|
|---|
| 15 | [travis-url]: https://travis-ci.org/node-modules/detect-port
|
|---|
| 16 | [codecov-image]: https://codecov.io/gh/node-modules/detect-port/branch/master/graph/badge.svg
|
|---|
| 17 | [codecov-url]: https://codecov.io/gh/node-modules/detect-port
|
|---|
| 18 | [download-image]: https://img.shields.io/npm/dm/detect-port.svg?style=flat-square
|
|---|
| 19 | [download-url]: https://npmjs.org/package/detect-port
|
|---|
| 20 |
|
|---|
| 21 | > JavaScript Implementation of Port Detector
|
|---|
| 22 |
|
|---|
| 23 | ## Usage
|
|---|
| 24 |
|
|---|
| 25 | ```bash
|
|---|
| 26 | $ npm i detect-port --save
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | ```js
|
|---|
| 30 | const detect = require('detect-port');
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * callback usage
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | detect(port, (err, _port) => {
|
|---|
| 37 | if (err) {
|
|---|
| 38 | console.log(err);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | if (port == _port) {
|
|---|
| 42 | console.log(`port: ${port} was not occupied`);
|
|---|
| 43 | } else {
|
|---|
| 44 | console.log(`port: ${port} was occupied, try port: ${_port}`);
|
|---|
| 45 | }
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * for a yield syntax instead of callback function implement
|
|---|
| 50 | */
|
|---|
| 51 |
|
|---|
| 52 | const co = require('co');
|
|---|
| 53 |
|
|---|
| 54 | co(function *() {
|
|---|
| 55 | const _port = yield detect(port);
|
|---|
| 56 |
|
|---|
| 57 | if (port == _port) {
|
|---|
| 58 | console.log(`port: ${port} was not occupied`);
|
|---|
| 59 | } else {
|
|---|
| 60 | console.log(`port: ${port} was occupied, try port: ${_port}`);
|
|---|
| 61 | }
|
|---|
| 62 | });
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * use as a promise
|
|---|
| 66 | */
|
|---|
| 67 |
|
|---|
| 68 | detect(port)
|
|---|
| 69 | .then(_port => {
|
|---|
| 70 | if (port == _port) {
|
|---|
| 71 | console.log(`port: ${port} was not occupied`);
|
|---|
| 72 | } else {
|
|---|
| 73 | console.log(`port: ${port} was occupied, try port: ${_port}`);
|
|---|
| 74 | }
|
|---|
| 75 | })
|
|---|
| 76 | .catch(err => {
|
|---|
| 77 | console.log(err);
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | ## Command Line Tool
|
|---|
| 83 |
|
|---|
| 84 | ```shell
|
|---|
| 85 | $ npm i detect-port -g
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | ### Quick Start
|
|---|
| 89 |
|
|---|
| 90 | ```shell
|
|---|
| 91 | # get an available port randomly
|
|---|
| 92 | $ detect
|
|---|
| 93 |
|
|---|
| 94 | # detect pointed port
|
|---|
| 95 | $ detect 80
|
|---|
| 96 |
|
|---|
| 97 | # more help
|
|---|
| 98 | $ detect --help
|
|---|
| 99 | ```
|
|---|
| 100 |
|
|---|
| 101 | ## Authors
|
|---|
| 102 |
|
|---|
| 103 | - [xudafeng](//github.com/xudafeng)
|
|---|
| 104 | - [zenzhu](//github.com/zenzhu)
|
|---|
| 105 |
|
|---|
| 106 | ## License
|
|---|
| 107 |
|
|---|
| 108 | [MIT](LICENSE)
|
|---|