How does express.js know its in live or development?

I've seen this following IF logic before, even used it, but with having no Node.js actively live I've never known exactly how it "knows"

// development only
if ('development' == app.get('env')) app.use(express.errorHandler());

A little dig through Express.js on Github and it seems that it comes down to whether a process.env.NODE_DEV is set. Look around line 50 (look for)

this.set('env', process.env.NODE_ENV || 'development');

To test this I wrote a simple node app called "whatEnvironment.js" with one console.log line and it returned "undefined"

A quick change the to the call (to set the environment variable) $ NODE_ENV=Production node whatEnvironment.js and this returned "Production"

When it comes to splitting out your servers (Dev, Staging, and Live) you can set NODE_ENV={environment} in the machine's ~/.bash_profile (for Mac)


comments powered by Disqus