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