diff --git a/lib/application.js b/lib/application.js index a66b23ee..a7fb511c 100644 --- a/lib/application.js +++ b/lib/application.js @@ -7,6 +7,7 @@ var DataSource = require('loopback-datasource-juggler').DataSource , compat = require('./compat') , assert = require('assert') , fs = require('fs') + , _ = require('underscore') , RemoteObjects = require('strong-remoting') , swagger = require('strong-remoting/ext/swagger') , stringUtils = require('underscore.string') @@ -425,15 +426,16 @@ app.boot = function(options) { process.env.npm_package_config_host || app.get('host'); - appConfig.port = - process.env.npm_config_port || - process.env.OPENSHIFT_SLS_PORT || - process.env.OPENSHIFT_NODEJS_PORT || - process.env.PORT || - appConfig.port || - process.env.npm_package_config_port || - app.get('port') || - 3000; + appConfig.port = _.find([ + process.env.npm_config_port, + process.env.OPENSHIFT_SLS_PORT, + process.env.OPENSHIFT_NODEJS_PORT, + process.env.PORT, + appConfig.port, + process.env.npm_package_config_port, + app.get('port'), + 3000 + ], _.isFinite); appConfig.restApiRoot = appConfig.restApiRoot || diff --git a/test/app.test.js b/test/app.test.js index 80268f50..47f31795 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -197,6 +197,12 @@ describe('app', function() { var app = this.boot(); assert.equal(app.get('host'), process.env.npm_config_host); + delete process.env.npm_config_host; + delete process.env.OPENSHIFT_SLS_IP; + delete process.env.OPENSHIFT_NODEJS_IP; + delete process.env.HOST; + delete process.env.npm_package_config_host; + process.env.npm_config_port = randomPort(); process.env.OPENSHIFT_SLS_PORT = randomPort(); process.env.OPENSHIFT_NODEJS_PORT = randomPort(); @@ -206,6 +212,12 @@ describe('app', function() { var app = this.boot(); assert.equal(app.get('host'), process.env.npm_config_host); assert.equal(app.get('port'), process.env.npm_config_port); + + delete process.env.npm_config_port; + delete process.env.OPENSHIFT_SLS_PORT; + delete process.env.OPENSHIFT_NODEJS_PORT; + delete process.env.PORT; + delete process.env.npm_package_config_port; }); function randomHost() { @@ -215,6 +227,18 @@ describe('app', function() { function randomPort() { return Math.floor(Math.random() * 10000); } + + it('should honor 0 for free port', function () { + var app = loopback(); + app.boot({app: {port: 0}}); + assert.equal(app.get('port'), 0); + }); + + it('should default to port 3000', function () { + var app = loopback(); + app.boot({app: {port: undefined}}); + assert.equal(app.get('port'), 3000); + }); }); it('Instantiate models', function () {