nodejs express: hostname with port from the req object
<p>Sometimes I want to sent a qualified URL in a mail from my node application. Or I would like to set the content of a phantom page object.</p>

<p>I get the full URL like this in my deployment setup.</p>

<pre><code>'http://' + req.hostname + '/myapp'
</code></pre>

<p>However on the development machine this usually produces:</p>

<p><a href="http://localhost/myapp" rel="noreferrer">http://localhost/myapp</a> instead off <a href="http://localhost:3000/myapp" rel="noreferrer">http://localhost:3000/myapp</a></p>

<p>I know how to get the port but I don't want to use something like:</p>

<pre><code>'http://' + req.hostname + ':' + port + '/myapp'
</code></pre>

<p>Which would produce nonsense like this in deploy behind a proxy.</p>

<p><a href="http://domain.com:3000/myapp" rel="noreferrer">http://domain.com:3000/myapp</a></p>

<p>Is there a smart way to get the hostname with port from the request object if the app runs outside a proxy?</p>