Node.js implementation of port detector
Who are using
-
⭐ ⭐ ⭐ eggjs/egg -
⭐ ⭐ ⭐ alibaba/ice -
⭐ ⭐ ⭐ alibaba/uirecorder -
⭐ ⭐ ⭐ facebook/create-react-app -
⭐ ⭐ ⭐ facebook/flipper -
⭐ ⭐ ⭐ umijs/umi -
⭐ ⭐ ⭐ gatsbyjs/gatsby -
⭐ ⭐ ⭐ electron-react-boilerplate/electron-react-boilerplate -
⭐ ⭐ ⭐ zeit/micro -
⭐ ⭐ ⭐ rails/webpacker -
⭐ ⭐ ⭐ storybookjs/storybook
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 |
chnliquan |
popomore |
---|---|---|---|---|---|
iamyinyue |
This project follows the git-contributor spec, auto updated at Tue Jul 30 2019 22:07:39 GMT+0800
.