NodeJS + ExpressJS - Unable to read environment variable

I am setting a variable in express as follows:

app.set('HOST', 'demo.sample.com');

However, if I try to read this variable, I get undefined as the output. I am reading the variable using process.env.HOST. However, if I try to read it using app.get('HOST'), I get the correct value - how do I get the value usingprocess.env.HOST`?

I cannot use `app.get('HOST') since I am reading the variable in another file too - a file that does not contain a reference to the express app variable.

Regards, Mithun

app.set() doesn't set environment variables, it's just a convenience method offered by Express to be used together with app.get().

If you want to set an environment variable in your JS code, use this:

process.env.HOST = 'demo.sample.com';

Update: process.env only reads your operating system environment settings. To setup express vars, use app.get() and app.set(), like in the other answer.

Is your HOST variable set at all? It's usually set as HOSTNAME.

[zlatko@droplet ~]$ node
> process.env.HOST
undefined
> process.env.HOSTNAME
'droplet.zlayer.net'
>

You could set it manually in Bash (I assume you use bash) though:

[zlatko@droplet ~]$ export HOST=$HOSTNAME
[zlatko@droplet ~]$ node
> process.env.HOST
'droplet.zlayer.net'

>

The export line could have been a literal, ie:

export HOSTNAME=myhostname.myserver.com

If you are on Linux you will need to set the environment variable for the bash/shell in order to call it from the APP. Try setting the environment variable using export NODE_ENV='production' or 'development' then configure your app using app.configure 'production',() -> and try again