Skip to content

gyllstromk/node-find-and-bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Find and bind to free port in node's restify/express/http

Resources

License

Stars

Watchers

Forks

Packages

No packages published