Skip to content

Latest commit

 

History

History
58 lines (40 loc) · 1.17 KB

README.md

File metadata and controls

58 lines (40 loc) · 1.17 KB

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();
        });
    });
});