title: How to find out the hostname of an application deployed on heroku
body: <p>We're developing an application that sends links for email verification, password reset etc. We would like to be able to set the host name part of the url dynamically, so that deploying to a staging instance would produce links that point back to the right thing. </p>
  
  <p>Is there a method to find out what my public hostname on a heroku instance is? I tried hostname and dnsdomainname but both returned a cryptic string, not the real public hostname. </p>
  
  <p>I tried the following approach in the server.js of my node.js application:</p>
  
  <pre><code>var dns = require('dns');
  var getIP = require('external-ip')();
  
  function getPublicDomain() {
    return new Promise((resolve, reject) =&gt; {
  
      // Start by resolving external IP address
      getIP(function (err, ip) {
        if (err) {
          throw err;
        }
        console.log(`External IP address: ${ip}`);
        console.log(`Performing reverse DNS lookup`);
  
        // Do reverse DNS lookup on the public IP
        dns.reverse(ip, function (err, domains) {
          if (err != null) {
            callback(err);
          }
          var hostname = null;
  
          async.forEach(domains,
            function (domain, callback) {
              dns.lookup(domain, function (err, address, family) {
                console.log(domain, '[', address, ']');
                if (ip == address) {
                  hostname = domain;
                }
                callback();
              });
            },
            function (error) {
              if (error) {
                reject(error);
              } else {
                resolve(hostname);
              }
            });
        });
      });
    });
  }
  
  getPublicDomain()
    .then((domain) =&gt; {
      console.log(`Public domain name: ${domain}`);
      process.env.DOMAIN=domain
    })
    .catch((error) =&gt; {
      console.error(`Could not determine public domain name`);
      console.error(error);
    });
  </code></pre>
  
  <p>The output was not quite what I expected:</p>
  
  <p>External IP address: ZZZ.ZZZ.ZZZ.ZZZ
  Performing reverse DNS lookup
  ec2-XX-XX-XXX-XXX.eu-west-1.compute.amazonaws.com [ YYY.YYY.YYY.YYY ]
  Public domain name: null</p>
  
  <p>(IPs are obscured with X / Y / Z where the X,Y,Z are all different!)</p>