title: How to run multiple server labels on same port using node and hapi?
body: <p>I am trying to host api and web servers on the same node code stack. I have used labels in order to apply configurations independently to each server but only one server works. Below is the code:</p>
  
  <pre><code>var hapi = require('hapi');
  
  // server definition
  var server = new hapi.Server();
  var runningPort = process.env.PORT || 3000;
  
  // setting up connection
  server.connection({
      host: '0.0.0.0',
      port: runningPort,
      labels: ['api']
  });
  
  server.connection({
      host: '0.0.0.0',
      port: runningPort,
      labels: ['web']
  });
  
  
  
  var webServer = server.select('web');
  var apiServer = server.select('api');
  
  // registering view engine
  webServer.views({
      engines: { html: require('handlebars') },
      relativeTo: __dirname,
      path: './views',
      layoutPath: './views/layout',
      layout: 'default',
      partialsPath: './views/partials'
  });
  
  // registering hapi auth cookie and application authentication
  webServer.register(
    {
      register: require('hapi-auth-cookie')
    }, 
    function (err) {
      if (err) {
        throw err;
      }
  
      var cache = webServer.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
      webServer.app.cache = cache;
  
      webServer.auth.strategy('session', 'cookie', true, {
          password: 'secret',
          cookie: 'sid-example',
          redirectTo: '/account/login',
          isSecure: false
      });
  });
  
  // registrations for api server
  apiServer.register(
          {
              register: require('lout')
          },
      function (err) {
          if (err) {
              throw err;
          }
  });
  
  apiServer.register(require('hapi-auth-bearer-token'), function (err) {
  
      apiServer.auth.strategy('simple', 'bearer-access-token', {
          allowQueryToken: true,              // optional, true by default
          allowMultipleHeaders: false,        // optional, false by default
          accessTokenName: 'access_token',    // optional, 'access_token' by default
          validateFunc: function( token, callback ) {
  
              // For convenience, the request object can be accessed
              // from `this` within validateFunc.
              var request = this;
  
              // Use a real strategy here,
              // comparing with a token from your database for example
              if(token === "1234"){
                  //## user object to be looked up here
                  callback(null, true, { token: token })
              } else {
                  callback(null, false, { token: token })
              }
          }
      });
  });
  
  //To do something to request before they passed on to routes
  apiServer.ext('onRequest', function (request, reply) {
      //## we can get user object here off of the authToken
      utils.log('info', 'apiCall', {method: request.method, path: request.path})
      return reply.continue();
  });
  
  // register routes
  webServer.route(webRoutes);
  apiServer.route(apiRoutes);
  
  server.start(function () {
      console.log('Web servers running at: ', 'localhost:' + runningPort);
      console.log('Api server running at: ', 'localhost:' + runningPort);
  });
  </code></pre>
  
  <p>Currently only api routes work.</p>