Irina Glushko 2389e7160b HW1 done | %!s(int64=3) %!d(string=hai) anos | |
---|---|---|
.. | ||
.vscode | %!s(int64=3) %!d(string=hai) anos | |
bin | %!s(int64=3) %!d(string=hai) anos | |
lib | %!s(int64=3) %!d(string=hai) anos | |
node_modules | %!s(int64=3) %!d(string=hai) anos | |
.eslintignore | %!s(int64=3) %!d(string=hai) anos | |
.eslintrc | %!s(int64=3) %!d(string=hai) anos | |
CONTRIBUTING.md | %!s(int64=3) %!d(string=hai) anos | |
HISTORY.md | %!s(int64=3) %!d(string=hai) anos | |
LICENSE | %!s(int64=3) %!d(string=hai) anos | |
README.md | %!s(int64=3) %!d(string=hai) anos | |
appveyor.yml | %!s(int64=3) %!d(string=hai) anos | |
index.js | %!s(int64=3) %!d(string=hai) anos | |
logo.png | %!s(int64=3) %!d(string=hai) anos | |
package.json | %!s(int64=3) %!d(string=hai) anos |
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