node.js端口_Node.js端口扫描程序

node.js端口

Node.js has become an incredible tool for creating services or utilities that act like a service.  Usually it's npm start, wait a moment, and you'll see the utility provide an address and port; a good example being localhost:8000.  One thing that bugs me about this pattern is if you have many service-based utilities that you work on, you wind up running into "port in use" errors, after which you need to look through all of your utilities to see which one to turn off.

Node.js已经成为创建服务或充当服务的实用程序的不可思议的工具。 通常是npm start ,稍等片刻,您会看到该实用程序提供了一个地址和端口。 一个很好的例子是localhost:8000 。 让我对此模式感到困惑的一件事是,如果您使用的是许多基于服务的实用程序,最终会遇到“端口使用中”错误,然后您需要查看所有实用程序以查看要使用哪个实用程序。关掉。

There's an easy solution to this problem:  Node Port Scanner.  This utility provides methods for finding in use or available ports on a given host!

有一个简单的解决方案: Node Port Scanner 。 该实用程序提供查找给定主机上正在使用或可用端口的方法!

使用端口扫描器 (Using Port Scanner)

The most common use case to solve port collisions would be findAPortNotInUse:

解决端口冲突的最常见用例是findAPortNotInUse


var portscanner = require('portscanner');

// 127.0.0.1 is the default hostname; not required to provide
portscanner.findAPortNotInUse([3000, 3010], '127.0.0.1').then(port => {
  console.log(`Port ${port} is available!`);

  // Now start your service on this port...
});


Providing a series of ports and then starting on the first available port is made simple -- no more collisions.

提供一系列端口,然后从第一个可用端口开始很简单-不再发生冲突。

You can also check for a given port's status, or check for ports in use:

您还可以检查给定端口的状态,或检查正在使用的端口:


// Get port status
portscanner.checkPortStatus(3000, '127.0.0.1').then(status => {
  // Status is 'open' if currently in use or 'closed' if available
  console.log(status);
});

// Find port in use
portscanner.findAPortInUse([3000, 3005, 3006], '127.0.0.1').then(port => {
  console.log('PORT IN USE AT: ' + port);
});


Using this port scanner utility is incredibly simple and the easiest way to get your service to run on any available port.  Hardcoded port usage, when unnecessary, only leads to frustration!

使用此端口扫描程序实用程序非常简单,并且是使服务在任何可用端口上运行的最简单方法。 不必要的硬编码端口用法只会导致失败!

翻译自: https://davidwalsh.name/javascript-port-scanner

node.js端口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值