我正在尝试将Meteor应用程序部署到AWS Elastic Beanstalk。它们不支持JSON对象作为环境变量,因此需要有一种解决方法。我正在将settings.json
文件上传到/tmp/settings.json
并将路径保存为process.env.METEOR_SETTINGS_FILE
然后将其保存在服务器文件中,我执行以下操作:
if (Meteor.isProduction) {
var meteorFile = process.env.METEOR_SETTINGS_FILE;
if(meteorFile == undefined) throw new Error(
'METEOR_SETTINGS_FILE env variable must be defined in production.')
var fs = Npm.require('fs');
var pjsonBuf = fs.readFileSync( meteorFile );
Meteor.settings = JSON.parse( pjsonBuf.toString().trim());
Meteor.settings.public = Meteor.settings.public;
}
然而,在客户端上,我仍然得到Uncaught TypeError: Cannot read property 'writeKey' of undefined
,其中未定义需要Meteor.settings.public.segment
。我有一个空对象Meteor.settings.public
,如果我在上面的文件中console.log(Meteor.settings)
,那么它们就在那里。
答案 0 :(得分:0)
以下是使用环境变量的解决方案:
据推测,Elastic Beanstalk允许您在shell脚本或配置文件中指定环境变量。我的例子使用Nginx / Passenger。
# Tell the app where MongoDB is
passenger_env_var MONGO_URL mongodb://localhost:27017/amba;
# Tell the app what its other settings are
passenger_env_var ROOT_URL https://dev1.amba.online;
passenger_env_var PEERJS_SERVER https://member2.myserver.com:9002/n5rdmnddd8q2z3q5mi;
passenger_env_var IMAGE_DIR /var/www/imgserver/public;
在服务器中,meteor方法读取环境变量并将它们插入表中:
// Get a list of environment vars and put them in the Environs collection
// - client code will pick these up and handle them.
getEnvirons: function() {
Environs.remove({}); // Force them to be re-read every time
var toGet = ["PEERJS_SERVER", "ROOT_URL", "IMAGE_DIR"];
_.each(toGet, function(e){
var val = process.env[e];
Environs.insert({name: e, value: val});
});
},
从服务器中的Meteor启动块调用上述内容。
剩下要做的就是发布Environs系列并订阅它。 (我假设你知道该怎么做)。当然,加载环境变量数据将是异步的,因此在代码中允许这样做