Skip to content

Commit ffabc33

Browse files
author
Eran Hammer
committed
Merge pull request hapijs#424 from wpreul/feature/address
Server uri isn't set until after server is started and now using node host
2 parents 0ece808 + deb20a6 commit ffabc33

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

lib/server.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ module.exports = internals.Server = function (/* host, port, options */) {
5858

5959
this._started = false;
6060
this.settings = Utils.applyToDefaults(Defaults.server, args.options);
61-
this.settings.host = args.host ? args.host.toLowerCase() : 'localhost';
61+
this.settings.host = args.host ? args.host.toLowerCase() : '0.0.0.0';
6262
this.settings.port = typeof args.port !== 'undefined' ? args.port : (this.settings.tls ? 443 : 80);
63-
this.settings.nickname = this.settings.host + ':' + this.settings.port;
64-
this.settings.uri = (this.settings.tls ? 'https://' : 'http://') + this.settings.host + ':' + this.settings.port;
63+
64+
if (this.settings.port) {
65+
this.settings.nickname = this.settings.host + ':' + this.settings.port;
66+
this.settings.uri = (this.settings.tls ? 'https://' : 'http://') + this.settings.host + ':' + this.settings.port;
67+
}
6568

6669
// Set optional configuration
6770
// false -> null, true -> defaults, {} -> override defaults
@@ -295,7 +298,10 @@ internals.Server.prototype.start = function (callback) {
295298
this.listener.once('listening', function () {
296299

297300
// update the port and uri with what was actually bound
298-
self.settings.port = self.listener.address().port;
301+
var address = self.listener.address();
302+
self.settings.port = address.port;
303+
self.settings.host = self.settings.host || address.address;
304+
self.settings.nickname = self.settings.host + ':' + self.settings.port;
299305
self.settings.uri = (self.settings.tls ? 'https://' : 'http://') + self.settings.host + ':' + self.settings.port;
300306

301307
Log.event('info', self.settings.nickname + ': Instance started at ' + self.settings.uri);

test/integration/response.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,16 +1177,21 @@ describe('Response', function () {
11771177
}
11781178
};
11791179

1180-
var server = new Hapi.Server();
1180+
var server = new Hapi.Server(0);
11811181
server.addRoute({ method: 'GET', path: '/redirect', config: { handler: handler } });
11821182

1183+
before(function (done) {
1184+
1185+
server.start(done);
1186+
});
1187+
11831188
it('returns a redirection reply', function (done) {
11841189

11851190
server.inject({ method: 'GET', url: '/redirect' }, function (res) {
11861191

11871192
expect(res.result).to.exist;
11881193
expect(res.result).to.equal('You are being redirected...');
1189-
expect(res.headers['Location']).to.equal('http://localhost:80/example');
1194+
expect(res.headers['Location']).to.equal(server.settings.uri + '/example');
11901195
expect(res.statusCode).to.equal(302);
11911196
done();
11921197
});
@@ -1198,7 +1203,7 @@ describe('Response', function () {
11981203

11991204
expect(res.result).to.exist;
12001205
expect(res.result).to.equal('We moved!');
1201-
expect(res.headers['Location']).to.equal('http://localhost:80/examplex');
1206+
expect(res.headers['Location']).to.equal(server.settings.uri + '/examplex');
12021207
expect(res.statusCode).to.equal(302);
12031208
done();
12041209
});

test/unit/server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ describe('Server', function () {
4848
done();
4949
});
5050

51-
it('defaults to localhost when no host is provided', function (done) {
51+
it('defaults to 0.0.0.0 when no host is provided', function (done) {
5252

53-
var server = new Hapi.Server();
54-
expect(server.settings.host).to.be.equal('localhost');
55-
done();
53+
var server = new Hapi.Server(0);
54+
server.start(function () {
55+
56+
expect(server.settings.host).to.be.equal('0.0.0.0');
57+
done();
58+
});
5659
});
5760

5861
it('doesn\'t throw an error when host and port are provided', function (done) {

0 commit comments

Comments
 (0)