更改要通过代码打开的新端口的状态

Change status of new port to be opend by code

我使用以下工作正常的代码来提供可用端口

portscanner.findAPortNotInUseAsync(55001, 65000, 'localhost').
then(function (port) {
        ...

应用程序实时占用可用端口,将端口状态更改为 open,如何模拟并将端口状态更改为打开 通过代码

我需要它进行内部测试...

当我尝试 Amina 的建议时,我遇到了错误参数打开状态

这是来自 webstrom 的屏幕截图

函数findAPortNotInUseAsync在内部使用这里定义的函数checkPortStatushttps://github.com/baalexander/node-portscanner/blob/master/lib/portscanner.js#L93

您可以fork这个项目,并为您想要设置为开放的端口添加例外。 例如:

在模块 portscanner.js 文件中:

portscanner.manualOpenPort={}
portscanner.setPortToOpen=function(port){
  portscanner.manualOpenPort[port]=true
}

在你的程序中:

portscanner.setPortToOpen(5050)

现在您需要将此行添加到原始 checkPortStatus 函数中:

portscanner.checkPortStatus = function(port, options, callback) {
  if (portscanner.manualOpenPort[port]) {
        callback(null,'open');
        return
  }