diff --git a/lib/application.js b/lib/application.js index 3b6d9dc6..06bf2610 100644 --- a/lib/application.js +++ b/lib/application.js @@ -879,6 +879,13 @@ app.listen = function(cb) { server.on('listening', function() { self.set('port', this.address().port); + if (!self.get('url')) { + // A better default host would be `0.0.0.0`, + // but such URL is not supported by Windows + var host = self.get('host') || '127.0.0.1'; + var url = 'http://' + host + ':' + self.get('port') + '/'; + self.set('url', url); + } }); var useAppConfig = diff --git a/test/app.test.js b/test/app.test.js index 139e24ea..5fbdeb27 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -481,6 +481,18 @@ describe('app', function() { }); }); + it('updates "url" on "listening" event', function(done) { + var app = loopback(); + app.set('port', 0); + app.set('host', undefined); + + app.listen(function() { + expect(app.get('url'), 'url') + .to.equal('http://127.0.0.1:' + app.get('port') + '/'); + done(); + }); + }); + it('forwards to http.Server.listen on more than one arg', function(done) { var app = loopback(); app.set('port', 1);