Skip to content

Instantly share code, notes, and snippets.

@tcorral
Forked from timoxley/isPortTaken.js
Created May 8, 2013 11:11
Show Gist options
  • Save tcorral/5539799 to your computer and use it in GitHub Desktop.
Save tcorral/5539799 to your computer and use it in GitHub Desktop.
var isPortTaken = function(PORT, callback) {
var net = require('net')
var tester = net.createServer()
tester.once('error', function (err) {
if (err.code == 'EADDRINUSE') {
callback(null, true)
} else {
callback(err)
}
})
tester.once('listening', function() {
tester.once('close', function() {
callback(null, false)
})
tester.close()
})
tester.listen(PORT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment