javascript 让 Heroku 的配置变量在 node.js 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13200810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Heroku's config vars to work in node.js
提问by Michael
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-facebook
节点包一起使用来定义来自 Facebook 身份验证的回调:
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 似乎不知道,process.env.IP
所以我继续config var
在 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.
调试服务器我看到 ip 不是我想要的,但它http://localhost
与我在第一行代码中定义的回退相同。
How do I get node to read the config var correctly from heroku ?
如何让节点从 heroku 正确读取配置变量?
回答by Michelle Tilley
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 applicationfor more information.
你想使用heroku config:set IP=http://app.mydomain.com
; 它定义了一个名为 的环境变量IP
,您可以在 Node.js 中通过process.env.IP
. 有关更多信息,请参阅为已部署的应用程序设置配置变量。