A nodejs library to get an available port
npm install port-me
Requires nodejs v6 or greater
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}`))
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}`)
})
The minimum number to use to find a port. This returned port will be equal or greater than this value.
The maximum number to use to find a port. This returned port will be equal or less than this value.
The maximum number of times portMe will attempt to find a port between the specified min and max values
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.