Skip to content

引入

ts
import { getValidPort } from 't-comm';

// 不支持 tree-shaking 的项目
import { getValidPort} from 't-comm/lib/node-port/index';

// 只支持 ESM 的项目
import { getValidPort} from 't-comm/es/node-port/index';

getValidPort(port)

描述

获取可用的端口号 从指定端口开始检测,如果端口被占用则自动尝试下一个端口,直到找到可用端口

参数

参数名描述
port

起始端口号

返回: Promise.<number>

  • 返回可用的端口号

示例

ts
// 从 3000 端口开始查找可用端口
getValidPort(3000).then(port => {
  console.log(`可用端口: ${port}`);
});