第三方包_detect port - dkvirus/npm-resource-read GitHub Wiki
说明
检测电脑上某个端口是否启动,如果启动了,自动返回一个已启动端口 + 1 的新端口。第三方包使用前先安装:$ npm install detect-port。
示例
电脑上 3400 端口未被占用,then() 的参数 _port 也就等于 3400;
电脑上 3400 端口被占用,then() 的参数 _port = port + 1 = 3401。
const detect = require('detect-port');
const port = 3400;
detect(port)
.then(_port => {
console.log(_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);
});