function checkPortStatus(port) { var args, host, opts, callback args = [].slice.call(arguments, 1) if (typeof args[0] === 'string') { host = args[0] } else if (typeof args[0] === 'object') { opts = args[0] } else if (typeof args[0] === 'function') { callback = args[0] } if (typeof args[1] === 'object') { opts = args[1] } else if (typeof args[1] === 'function') { callback = args[1] } if (typeof args[2] === 'function') { callback = args[2] } if (!callback) return promisify(checkPortStatus, arguments) opts = opts || {} host = host || opts.host || '127.0.0.1' var timeout = opts.timeout || 400 var connectionRefused = false var socket = new Socket() var status = null var error = null // Socket connection established, port is open socket.on('connect', function () { status = 'open' socket.destroy() }) // If no response, assume port is not listening socket.setTimeout(timeout) socket.on('timeout', function () { status = 'closed' error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available') socket.destroy() }) // Assuming the port is not open if an error. May need to refine based on // exception socket.on('error', function (exception) { if (exception.code !== 'ECONNREFUSED') { error = exception } else { connectionRefused = true } status = 'closed' }) // Return after the socket has closed socket.on('close', function (exception) { if (exception && !connectionRefused) { error = error || exception } else { error = null } callback(error, status) }) socket.connect(port, host) }
...
A brief example:
```javascript
var portscanner = require('portscanner')
// Checks the status of a single port
portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
console.log(status)
})
// Find the first available port. Asynchronously checks, so first port
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) {
...
function findAPortInUse() { var params = [].slice.call(arguments) params.unshift('open') return findAPortWithStatus.apply(null, params) }
...
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('AVAILABLE PORT AT: ' + port)
})
// Find the first port in use or blocked. Asynchronously checks, so first port
// to respond is returned.
portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
})
// You can also pass array of ports to check
portscanner.findAPortInUse([3000, 3005, 3006], '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
})
...
function findAPortNotInUse() { var params = [].slice.call(arguments) params.unshift('closed') return findAPortWithStatus.apply(null, params) }
...
portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
console.log(status)
})
// Find the first available port. Asynchronously checks, so first port
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port
) {
console.log('AVAILABLE PORT AT: ' + port)
})
// Find the first port in use or blocked. Asynchronously checks, so first port
// to respond is returned.
portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
...
function checkPortStatus(port) { var args, host, opts, callback args = [].slice.call(arguments, 1) if (typeof args[0] === 'string') { host = args[0] } else if (typeof args[0] === 'object') { opts = args[0] } else if (typeof args[0] === 'function') { callback = args[0] } if (typeof args[1] === 'object') { opts = args[1] } else if (typeof args[1] === 'function') { callback = args[1] } if (typeof args[2] === 'function') { callback = args[2] } if (!callback) return promisify(checkPortStatus, arguments) opts = opts || {} host = host || opts.host || '127.0.0.1' var timeout = opts.timeout || 400 var connectionRefused = false var socket = new Socket() var status = null var error = null // Socket connection established, port is open socket.on('connect', function () { status = 'open' socket.destroy() }) // If no response, assume port is not listening socket.setTimeout(timeout) socket.on('timeout', function () { status = 'closed' error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available') socket.destroy() }) // Assuming the port is not open if an error. May need to refine based on // exception socket.on('error', function (exception) { if (exception.code !== 'ECONNREFUSED') { error = exception } else { connectionRefused = true } status = 'closed' }) // Return after the socket has closed socket.on('close', function (exception) { if (exception && !connectionRefused) { error = error || exception } else { error = null } callback(error, status) }) socket.connect(port, host) }
...
A brief example:
```javascript
var portscanner = require('portscanner')
// Checks the status of a single port
portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
console.log(status)
})
// Find the first available port. Asynchronously checks, so first port
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) {
...
function findAPortInUse() { var params = [].slice.call(arguments) params.unshift('open') return findAPortWithStatus.apply(null, params) }
...
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('AVAILABLE PORT AT: ' + port)
})
// Find the first port in use or blocked. Asynchronously checks, so first port
// to respond is returned.
portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
})
// You can also pass array of ports to check
portscanner.findAPortInUse([3000, 3005, 3006], '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
})
...
function findAPortNotInUse() { var params = [].slice.call(arguments) params.unshift('closed') return findAPortWithStatus.apply(null, params) }
...
portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
console.log(status)
})
// Find the first available port. Asynchronously checks, so first port
// determined as available is returned.
portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port
) {
console.log('AVAILABLE PORT AT: ' + port)
})
// Find the first port in use or blocked. Asynchronously checks, so first port
// to respond is returned.
portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
...