Veck Hsiao
Veck Hsiao

Reputation: 627

How to recognize "heroku" in Node.js?

I've deployed an app to heroku and I want to make this app recognize platform information by itself. That is,

var dburi = '';
if( is_heroku )
  dburi = process.env.MONGOLAB_URI;
else
  dburi = 'mongodb://xxx'; 

Is there a way to get recognition (flag?) "is_heroku"?

Thanks!

Upvotes: 0

Views: 26

Answers (1)

hunterloftis
hunterloftis

Reputation: 13799

Just use a guard. It doesn't matter whether or not you're on Heroku:

var dburi = process.env.MONGOLAB_URI || 'mongodb://xxx';

That way, if you're in an environment that specifies MONGOLAB_URI, you use it, and if not you fall back to some default (for local development, for instance).

Another benefit of this is that you can point your app to connect to whichever db you like, eg:

MONGOLAB_URI='mongodb://some-db-in-the-cloud' node server.js

Upvotes: 1

Related Questions