Find and bind to free port in node's restify/express/http
JavaScript
Switch branches/tags
Nothing to show
Latest commit cae93a4 Sep 4, 2013 @gyllstromk updated README
Permalink
Failed to load latest commit information.
test
.gitignore
.npmignore
LICENSE
README.md
index.js
package.json

README.md

Find and bind

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

$ npm install findandbind

findandbind is ideal for test scenarios in which hard coded ports are not desirable.

Usage

var app = restify.createServer(); // or app = express(), etc

// ... do my configuration of app

var findandbind = require('findandbind');

findandbind(app, function (err, port) {
    // starts looking at 1024 ...
    console.log('listening on', port);
});

Specifying port from which to start checking

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

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

In tests

describe('testing my server', function () {
    var myUrl;

    before(function(done) {
        findandbind(app, function (err, port) {
            myUrl = 'http://localhost:' + port;
            done(err);
        });
    });

    it('GET / returns 200', function (done) {
        request(myUrl, function (err, resp, body) {
            assert.equal(resp.statusCode, 200);
            done();
        });
    });
});