Irina Glushko 2389e7160b HW1 done | il y a 3 ans | |
---|---|---|
.. | ||
.vscode | il y a 3 ans | |
bin | il y a 3 ans | |
lib | il y a 3 ans | |
node_modules | il y a 3 ans | |
.eslintignore | il y a 3 ans | |
.eslintrc | il y a 3 ans | |
CONTRIBUTING.md | il y a 3 ans | |
HISTORY.md | il y a 3 ans | |
LICENSE | il y a 3 ans | |
README.md | il y a 3 ans | |
appveyor.yml | il y a 3 ans | |
index.js | il y a 3 ans | |
logo.png | il y a 3 ans | |
package.json | il y a 3 ans |
JavaScript Implementation of Port Detector
$ 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);
});
$ npm i detect-port -g
# get an available port randomly
$ detect
# detect pointed port
$ detect 80
# more help
$ detect --help