/**
* 获取可用端口
* @param ports 既定的端口范围,从该端口范围寻找空闲端口
*/
module.exports.getFreePort = function(ports) {
if (ports.length === 0) {
log.info('分配端口已达最大限额')
return null
}
const os = require('os');
var port = ports.shift()
const {execSync} = require('child_process')
var stdout = null
if (os.type() === 'Windows_NT') {
//windows
} else if (os.type() === 'Darwin') {
//mac
stdout = execSync(`lsof -i:${port}`)
} else if (os.type() === 'Linux') {
//Linux
stdout = execSync(`netstat -anp | grep ${port}`)
}
if (!stdout) {
return port
}else {
return getFreePort()
}
}
node区分mac\linux系统查找空闲可用端口
最新推荐文章于 2023-05-12 11:00:46 发布