I have to configure one of the module in my application and give it a specific URL, based on the hostname. Something like
http://localhost locally and
http://myapp.herokuapp.com. I know I can just hardcode the value depending on
NODE_ENV, or even store it as an environment value and use
process.env.CUSTOMVAR_HOSTNAME. But somehow it feels wrong, since
myapp is already configured in the Heroku admin panel. And I can't use
request.headers.host since the module configuration happens at the start of the application, before any request can be handled.
Is there a way to retrieve the Heroku application name from the code ? Or from an environment variable ? I want my code to stay the same whether I'm executing code locally or on Heroku (or any other place actually).
I didn't find any way, so I created a local variable
hostname based on NODE_ENV (
production) with the proper hostname value.
There is currently no discussion for this recipe.
This recipe can be found in it's original form on Stack Over Flow.