Skip to content

gavinvangent/port-me

Repository files navigation

JavaScript Style Guide Build Status Codacy Badge Codacy Badge GitHub issues GitHub stars npm

port-me

A nodejs library to get an available port

Installation

npm install port-me

Requires nodejs v6 or greater

How to use

Promises

To use all the defaults:

import portMe from 'port-me'

portMe()
  .then(port => console.log(`port = ${port}`))
  .catch(err => console.log(`error = ${err.message}`))

To use an options object instead:

import portMe from 'port-me'

const opts = {
  min: 1025,
  max: 65535,
  maxAttempts: 50
}

portMe(opts)
  .then(port => console.log(`port = ${port}`))
  .catch(err => console.log(`error = ${err.message}`))

Callbacks

To use all the defaults:

import portMe from 'port-me'

portMe((err, port) => {
  console.log(`port = ${port}`)
})

To specify min and max port:

import portMe from 'port-me'

const min = 5000
const max = 5050

portMe(min, max, (err, port) => {
  console.log(`port = ${port}`)
})

To specify a max attempt count:

import portMe from 'port-me'

const min = 5000
const max = 5050
const maxAttempts = 5

portMe(min, max, (err, port) => {
  console.log(`port = ${port}`)
}, maxAttempts)

To use an options object instead:

import portMe from 'port-me'

const opts = {
  min: 5000,
  max: 5050,
  matAttempts: 5
}

portMe(opts, (err, port) => {
  console.log(`port = ${port}`)
})

Options

min - number {default: 1025}

The minimum number to use to find a port. This returned port will be equal or greater than this value.

max - number {default: 65535}

The maximum number to use to find a port. This returned port will be equal or less than this value.

maxAttempts - number {default: 50}

The maximum number of times portMe will attempt to find a port between the specified min and max values

Caveat

There is a very tiny chance of a race condition if another service starts using the same port number as you in between the time you get the port number and you actually start using it.

About

Get an available port

Resources

License

Stars

Watchers

Forks

Packages

No packages published