Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Heroku's config vars to work in node.js

I have a node app that has a line like this:

var ip = process.env.IP || 'http://localhost';

I am using it with passport-facebook node package to define the callback from Facebook authentication:

passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: ip + ":" + port + "/auth/facebook/callback"
},
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {
      return done(null, profile);
    });
  }
));

It seems that Heroku doesn't know process.env.IP so I went ahead and defined a config var in Heroku:

heroku config:add process.env.IP=http://app.mydomain.com

Debugging the server I see that ip is not what I want but it's http://localhost same as I defined as the fallback in the first line of code.

How do I get node to read the config var correctly from heroku ?

like image 433
Michael Avatar asked Nov 02 '12 18:11

Michael


People also ask

How do I use heroku config vars in node JS?

You want to use heroku config:set IP=http://app.mydomain.com ; it defines an environment variable called IP , which you access in Node. js via process. env. IP .

Does .env work in heroku?

env file on Heroku isn't a good approach. Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable. But for testing these config vars in local, we need to set up a .

Is heroku config vars safe?

Heroku config vars are designed to be safe for storing sensitive information. All config vars are stored in an encrypted form and safely stored. These are only decrypted and loaded when booting your app in a dyno itself.


1 Answers

You want to use heroku config:set IP=http://app.mydomain.com; it defines an environment variable called IP, which you access in Node.js via process.env.IP. See Setting up config vars for a deployed application for more information.

like image 73
Michelle Tilley Avatar answered Sep 22 '22 20:09

Michelle Tilley