Created
February 16, 2012 01:33
-
-
Save mikeal/1840641 to your computer and use it in GitHub Desktop.
get a new/clean port with node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var portrange = 45032 | |
function getPort (cb) { | |
var port = portrange | |
portrange += 1 | |
var server = net.createServer() | |
server.listen(port, function (err) { | |
server.once('close', function () { | |
cb(port) | |
}) | |
server.close() | |
}) | |
server.on('error', function (err) { | |
getPort(cb) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ronen & @jonathantneal, reraise:
NB: edited to remove port range checks since server.listen has that check; only need to check that maxPort > port.