Node.js Port Scanner

By  on  

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.

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!

Using Port Scanner

The most common use case to solve port collisions 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...
});

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!

Recent Features

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

  • By
    Create a Simple Slideshow Using MooTools

    One excellent way to add dynamism to any website is to implement a slideshow featuring images or sliding content. Of course there are numerous slideshow plugins available but many of them can be overkill if you want to do simple slideshow without controls or events.

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!