Irina Glushko 2389e7160b HW1 done | vor 3 Jahren | |
---|---|---|
.. | ||
.vscode | vor 3 Jahren | |
bin | vor 3 Jahren | |
lib | vor 3 Jahren | |
node_modules | vor 3 Jahren | |
.eslintignore | vor 3 Jahren | |
.eslintrc | vor 3 Jahren | |
CONTRIBUTING.md | vor 3 Jahren | |
HISTORY.md | vor 3 Jahren | |
LICENSE | vor 3 Jahren | |
README.md | vor 3 Jahren | |
appveyor.yml | vor 3 Jahren | |
index.js | vor 3 Jahren | |
logo.png | vor 3 Jahren | |
package.json | vor 3 Jahren |
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