So, in my node.js 5.2.0 / express.js 4.2.0 I can do
if (app.get('env') === 'development') { app.use(//etc
or
var env = process.env.NODE_ENV || 'development'; if (env === 'development') { app.use(//etc
So process.env.NODE_ENV
and app.get('env')
both get the environment's value. Is there any significant difference besides the syntax?
Thanks
The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process. env.
js, process. env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process. env during runtime and can later be accessed.
So instead, you use process. env. PORT to tell your application to take the PORT by reading the Environment Variable. You put the || just to make sure that if the PORT variable by any chance was not found, use the specified port instead.
There is no significant difference.
Express app.get('env')
returns 'development'
if NODE_ENV is not defined. So you don't need the line to test its existence and set default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With