之前介绍的端口探测工具版本更新了,现在支持更多实用的用法。
# detect port 80
$ detect -p 80
# or like this
$ detect --port 80
# will get result below
$ port: 80 was occupied, try port: 1024
# with verbose
$ detect --port 80 --verbose
# more help?
$ detect -h
var detect = require('detect-port');
/**
* 普通用法
*/
detect(port, function(error, _port) {
if (error) {
console.log('Error');
return;
}
if (port === _port) {
console.log('port: %d was not occupied', port);
} else {
console.log('port: %d was occupied, try port: %d', port, _port);
}
});
/**
* 在co v3中使用
* for a yield syntax instead of callback function implement
*/
var co = require('co');
co(function *() {
var _port = yield detect(port);
if (port === _port) {
console.log('port: %d was not occupied', port);
} else {
console.log('port: %d was occupied, try port: %d', port, _port);
}
})();
/**
* 在co v4中使用
* for a yield syntax instead of callback function implement
*/
var co = require('co');
co(function *() {
var _port = yield detect(port);
if (port === _port) {
console.log('port: %d was not occupied', port);
} else {
console.log('port: %d was occupied, try port: %d', port, _port);
}
});
/**
* 作为 promise 使用
*/
var promisePort = detect(port);
promisePort.then(function(_port) {
if (port === _port) {
console.log('port: %d was not occupied', port);
} else {
console.log('port: %d was occupied, try port: %d', port, _port);
}
}).catch(function(err) {
console.log(err);
});
项目托管在 https://github.com/xudafeng/detect-port
联系方式:https://github.com/xudafeng/ 可以跟帖或者直接在 github 提出 issue。
欢迎大家使用并提出问题和建议,我们会积极跟进和修改,谢谢!