Skip to content

Commit

Permalink
server-node: Support for setting 'hostname' http option
Browse files Browse the repository at this point in the history
You can now select which hostname to listen on via

1: Command line option:

  $ node osjs run --hostname=::

2: Configuration

  $ node osjs config:set --name=server.http.hostname --value="::"
  $ node osjs build:config
  $ node osjs run
  • Loading branch information
andersevenrud committed May 22, 2017
1 parent 087e104 commit 3adf1a0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/build/index.js
Expand Up @@ -306,6 +306,7 @@ module.exports.run = function(cli, args) {
const settings = require(_path.join(ROOT, 'src/server/node/core/settings.js'));

const opts = {
HOSTNAME: cli.option('hostname'),
DEBUG: cli.option('debug'),
PORT: cli.option('port'),
LOGLEVEL: cli.option('loglevel')
Expand Down
1 change: 1 addition & 0 deletions src/conf/200-server.json
Expand Up @@ -32,6 +32,7 @@
"http": {
"mode": "http",
"connection": "%connection%",
"hostname": null,
"port" : 8000,
"compression": {
"memLevel": 8,
Expand Down
5 changes: 5 additions & 0 deletions src/server/node/core/env.js
Expand Up @@ -37,6 +37,7 @@ const _path = require('path');
*/

const ENV = {
HOSTNAME: null,
DEBUG: false,
PORT: null,
LOGLEVEL: -2,
Expand Down Expand Up @@ -93,6 +94,10 @@ module.exports.update = function(config) {
ENV.PORT = config.http.port;
}

if ( !ENV.HOSTNAME && config.http.hostname ) {
ENV.HOSTNAME = config.http.hostname;
}

if ( config.overlays ) {
Object.keys(config.overlays).forEach((name) => {
const overlay = config.overlays[name];
Expand Down
7 changes: 4 additions & 3 deletions src/server/node/core/http.js
Expand Up @@ -543,13 +543,14 @@ module.exports.init = function init() {
/**
* Runs the HTTP server
*
* @param {Number} port Which port number
* @param {Number} port Which port number
* @param {String} [hostname] Which hostname
*
* @function run
* @memberof core.http
*/
module.exports.run = function run(port) {
httpServer.listen(port);
module.exports.run = function run(port, hostname) {
httpServer.listen(port, hostname);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/server/node/core/instance.js
Expand Up @@ -395,7 +395,7 @@ module.exports.run = function run(port) {
_evhandler.emit('server:run');

_logger.log('INFO', _logger.colored('Starting OS.js server', 'green'));
_logger.log('INFO', _logger.colored(['Running', httpConfig.mode, 'on localhost:' + ENV.PORT].join(' '), 'green'));
_logger.log('INFO', _logger.colored(['Running', httpConfig.mode, 'on', (ENV.HOSTNAME || 'localhost') + ':' + ENV.PORT].join(' '), 'green'));

if ( httpConfig.connection === 'ws' ) {
const wsp = httpConfig.ws.port === 'upgrade' ? ENV.PORT : httpConfig.ws.port;
Expand All @@ -409,7 +409,7 @@ module.exports.run = function run(port) {
_logger.log('INFO', _logger.colored('Running in production mode', 'yellow'));
}

const result = _http.run(ENV.PORT);
const result = _http.run(ENV.PORT, ENV.HOSTNAME);
_logger.log('INFO', _logger.colored('Ready...', 'green'));

return result;
Expand Down
1 change: 1 addition & 0 deletions src/server/node/server.js
Expand Up @@ -46,6 +46,7 @@ const _minimist = require('minimist');
const argv = _minimist(process.argv.slice(2));
const opts = {
DEBUG: argv.debug,
HOSTNAME: argv.h || argv.hostname,
ROOT: argv.r || argv.root,
PORT: argv.p || argv.port,
LOGLEVEL: argv.l || argv.loglevel,
Expand Down

0 comments on commit 3adf1a0

Please sign in to comment.