lib/router/config-router.js
function createServers(conf) {
  var seenKeys = map.create();

  var servers = _.map(conf.search('server'), function(serverNode) {
    var listenNode = serverNode.getOne('listen');
    if (!listenNode)
      throwForNode(serverNode,
          new Error('Required "listen" directive is missing'));

    var port = serverNode.getValues('listen').port;
    if (typeof port === 'undefined')
      port = 80;
    if (port <= 0)
      throwForNode(listenNode, new Error('Invalid port number'));

    var host = serverNode.getValues('listen').host || '::';
    if (host === '*')
      host = '::';

    if (!iputil.isValid(host)) {
      throwForNode(listenNode,
          new Error('Invalid IP address "' + host + '"'));
    }

    if (iputil.hasZone(host)) {
      throwForNode(listenNode,
          new Error(`Invalid IP address "${host}"--zone IDs are not currently supported`))
    }

    var serverNames = serverNode.getValues('server_name').names;

    // Read all locations. Use post-order traversal so that nested locs
    // get priority over their parents.
    var locations = _.chain(serverNode.search('location', false, true))
    .map(createLocation)
    .compact()
    .value();

    // We get the templateDir at the server level so that a global or server-
    // wide templateDir can be attached to the request directly. We may need
    // this if a page gets returns without a matching AppSpec (like a 404).
    var templateDir = serverNode.getValues('template_dir').dir;

    _.each(serverNames || [''], function(serverName) {
      var key = host + ':' + port;
      if (serverName !== '')
        key += '(' + serverName + ')';
      if (seenKeys[key])
        throwForNode(listenNode,
            new Error(key + ' combination conflicts with earlier server definition'));
      seenKeys[key] = true;
    });

    return new ServerRouter(port, host, serverNames, locations, templateDir);
  });

  return servers;
}
function createServers(conf) {
  var seenKeys = map.create();

  var servers = _.map(conf.search('server'), function(serverNode) {
    var listenNode = serverNode.getOne('listen');
    if (!listenNode)
      throwForNode(serverNode,
          new Error('Required "listen" directive is missing'));

    var port = serverNode.getValues('listen').port;
    if (typeof port === 'undefined')
      port = 80;
    if (port <= 0)
      throwForNode(listenNode, new Error('Invalid port number'));

    var host = serverNode.getValues('listen').host || '::';
    if (host === '*')
      host = '::';

    if (!iputil.isValid(host)) {
      throwForNode(listenNode,
          new Error('Invalid IP address "' + host + '"'));
    }

    if (iputil.hasZone(host)) {
      throwForNode(listenNode,
          new Error(`Invalid IP address "${host}"--zone IDs are not currently supported`))
    }

    var serverNames = serverNode.getValues('server_name').names;

    // Read all locations. Use post-order traversal so that nested locs
    // get priority over their parents.
    var locations = _.chain(serverNode.search('location', false, true))
    .map(createLocation)
    .compact()
    .value();

    // We get the templateDir at the server level so that a global or server-
    // wide templateDir can be attached to the request directly. We may need
    // this if a page gets returns without a matching AppSpec (like a 404).
    var templateDir = serverNode.getValues('template_dir').dir;

    _.each(serverNames || [''], function(serverName) {
      var key = host + ':' + port;
      if (serverName !== '')
        key += '(' + serverName + ')';
      if (seenKeys[key])
        throwForNode(listenNode,
            new Error(key + ' combination conflicts with earlier server definition'));
      seenKeys[key] = true;
    });

    return new ServerRouter(port, host, serverNames, locations, templateDir);
  });

  return servers;
}
function createServers ( conf ) { var seenKeys = map . create ( ) ; var servers = _ . map ( conf . search ( 'server' ) , function ( serverNode ) { var listenNode = serverNode . getOne ( 'listen' ) ; if ( ! listenNode ) throwForNode ( serverNode , new Error ( 'Required "listen" directive is missing' ) ) ; var port = serverNode . getValues ( 'listen' ) . port ; if ( typeof port === 'undefined' ) port = 80 ; if ( port <= 0 ) throwForNode ( listenNode , new Error ( 'Invalid port number' ) ) ; var host = serverNode . getValues ( 'listen' ) . host || '::' ; if ( host === '*' ) host = '::' ; if ( ! iputil . isValid ( host ) ) { throwForNode ( listenNode , new Error ( 'Invalid IP address "' + host + '"' ) ) ; } if ( iputil . hasZone ( host ) ) { throwForNode ( listenNode , new Error ( ` ${ host } ` ) ) } var serverNames = serverNode . getValues ( 'server_name' ) . names ; // Read all locations. Use post-order traversal so that nested locs // get priority over their parents. var locations = _ . chain ( serverNode . search ( 'location' , false , true ) ) . map ( createLocation ) . compact ( ) . value ( ) ; // We get the templateDir at the server level so that a global or server- // wide templateDir can be attached to the request directly. We may need // this if a page gets returns without a matching AppSpec (like a 404). var templateDir = serverNode . getValues ( 'template_dir' ) . dir ; _ . each ( serverNames || [ '' ] , function ( serverName ) { var key = host + ':' + port ; if ( serverName !== '' ) key += '(' + serverName + ')' ; if ( seenKeys [ key ] ) throwForNode ( listenNode , new Error ( key + ' combination conflicts with earlier server definition' ) ) ; seenKeys [ key ] = true ; } ) ; return new ServerRouter ( port , host , serverNames , locations , templateDir ) ; } ) ; return servers ; }
Crawl the parsed and validated config file data to create an array of
ServerRouters.
Crawl the parsed and validated config file data to create an array of ServerRouters .