gpt4 book ai didi

meteor - 一开始如何在客户端和服务器中使用环境变量

转载 作者:行者123 更新时间:2023-12-04 22:11:25 25 4
gpt4 key购买 nike

当我像这样开始我的 meteor 项目时,我正在使用环境变量

MYVAR1="foo bar sdf" MYVAR2=0 meteor

然后在一个文件 [root]/lib/constants.js 我有
ENV = ["sjobs", "unisight", "dfgdfsgf"];
AUTHENTICATION = true;

if (Meteor.isClient) {
Meteor.call("getGlobals", function (error, result) {
"use strict";

if (error === undefined) {
AUTHENTICATION = result.AUTHENTICATION;
ENV= result.ENV;
console.log(result);
} else {
console.error(error);
}

});
}

if (Meteor.isServer) {
var univasENV = ["urb", "unisight", "sjobs", "unicloud"];
var tmpenv;


if (process.env.MYVAR2 !== undefined && parseInt(process.env.MYVAR2, 10) === 1) {
AUTHENTICATION = false;

}

if (process.env.MYVAR1 !== undefined) {
tmpenv = process.env.MYVAR1.split(" ");
ENV = [];
_.each(tmpenv, function (value) {
"use strict";

if (univasENV.indexOf(value) !== -1) {
ENV.push(value);

}
});
}
}

在另一个文件[root]/server/methods.js中,我有:
Meteor.methods({
getGlobals: function(){
"use strict";
console.log(AUTHENTICATION, ENV);
return {
auth: AUTHENTICATION,
env: ENV
};
}
});

服务器部分按我的预期工作,但是在渲染或加载所有内容后执行客户端中的代码。这里的问题是运行异步的 Meteor.call(),我不能使用(据我所知)Meteor.wrapAsync()。

我还尝试在集合中写入我需要的那些值,然后从客户端读取它(所有这些都在 [root]/lib/constants.js 中),但它的行为与方法/调用完全相同。

所以问题是如何在一开始就将一些值从服务器传递给客户端?

最佳答案

您似乎想在客户端 java 脚本中使用在服务器上设置的环境变量。我会考虑使用 Meteor.settings 以便您可以在启动 meteor 时在 key public 下的 json 中指定各种环境设置。这应该可用于您的客户端代码。

If the settings object contains a key named public, then Meteor.settings.public will be available on the client as well as the server. All other properties of Meteor.settings are only defined on the server. http://docs.meteor.com/#/full/meteor_settings



希望有帮助

关于meteor - 一开始如何在客户端和服务器中使用环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com