Skip to content

Instantly share code, notes, and snippets.

@ethanliew
Forked from timoxley/isPortTaken.js
Created December 13, 2013 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanliew/7942084 to your computer and use it in GitHub Desktop.
Save ethanliew/7942084 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