aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node/port_picker.js
blob: 7b3cfd74ff99fba7f374c64b71f8bda2b26f424a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var net = require('net');

/**
 * Finds a free port that a server can bind to, in the format
 * "address:port"
 * @param {function(string)} cb The callback that should execute when the port
 *     is available
 */
function nextAvailablePort(cb) {
  var server = net.createServer();
  server.listen(function() {
    var address = server.address();
    server.close(function() {
      cb(address.address + ':' + address.port.toString());
    });
  });
}

exports.nextAvailablePort = nextAvailablePort;