Node.js Port Scanner

Node.js has turned into an extraordinary instrument for making administrations or utilities that demonstration like an administration. Normally it’s npm begin, hold up a minute, and you’ll see the utility give a location and port; a genuine model being localhost:8000. One thing that bugs me about this example is on the off chance that you have many administration put together utilities that you work with respect to, you end up running into “port being used” mistakes, after which you have to glance through the majority of your utilities to see which one to kill.

There’s a simple answer for this issue: Hub Port Scanner. This utility gives techniques to finding being used or accessible ports on a given host!

Utilizing Port Scanner

The most well-known use case to fathom port crashes would be 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...
});

Giving a progression of ports and after that beginning the most readily accessible port is made basic – no more crashes.

You can likewise check for a given port’s status, or check for ports being used:

// 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);
});

Utilizing this port scanner utility is extraordinarily straightforward and the simplest method to inspire your administration to keep running on any accessible port. Hardcoded port utilization, when pointless, just prompts disappointment!