findandbind

Find and bind to free port in restify/express/connect.

Find and bind

Find a free port to which to bind your restify/express/http server.

$ npm install findandbind
 
var app = restify.createServer(); // or app = express(), etc 
 
// ... do my configuration of app 
 
var findandbind = require('findandbind');
 
findandbind(app, function (errport) {
    // starts looking at 1024 ... 
    console.log('listening on', port);
});

By default, findandbind starts checking at 1024, the first user port. To start checking at a different port:

 
findandbind(app, { start: 2048 }, function (errport) {
    console.log('listening on', port);
});