You have several options:
Update
From the request object, we can fetch both the host and the port, please check out these methods:
request.protocol
request.host
request.port
You could build the full url from these methods.
From my personal experience, for most projects you might have a fixed domain for each environment, and usually configure that in a yaml file or so (for example, to send email and use urls with domain name in the email body, we usually read the config and set options for url helper). So I usually just read the current environment's urls configuration and use them from the code.