Irina Glushko 2389e7160b HW1 done | пре 3 година | |
---|---|---|
.. | ||
.vscode | пре 3 година | |
bin | пре 3 година | |
lib | пре 3 година | |
node_modules | пре 3 година | |
.eslintignore | пре 3 година | |
.eslintrc | пре 3 година | |
CONTRIBUTING.md | пре 3 година | |
HISTORY.md | пре 3 година | |
LICENSE | пре 3 година | |
README.md | пре 3 година | |
appveyor.yml | пре 3 година | |
index.js | пре 3 година | |
logo.png | пре 3 година | |
package.json | пре 3 година |
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