Javascript 在 node.js 中将数组定义为环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31552125/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:49:02  来源:igfitidea点击:

Defining an array as an environment variable in node.js

javascriptnode.jsherokuenvironment-variablesenv

提问by paranoidhominid

I have an array that I pull data from.

我有一个从中提取数据的数组。

festivals = ['bonnaroo', 'lollapalooza', 'coachella']

Since I'm using heroku, it may be better to replace it with an environment variable, but I'm not sure how to do that.

由于我正在使用 heroku,因此最好将其替换为环境变量,但我不知道该怎么做。

Is using a JSON string as an environment variable the way to go?

使用 JSON 字符串作为环境变量是否可行?

回答by hunterloftis

In this scenario, it doesn't sound like an env var is the way to go.

在这种情况下,听起来不像是 env var 是要走的路。

Usually, you'll want to use environment variables to give your application information about its environment or to customize its behavior: which database to connect to, which auth tokens to use, how many workers to fork, whether or not to cache rendered views, etc.

通常,您需要使用环境变量来为您的应用程序提供有关其环境的信息或自定义其行为:要连接到哪个数据库、要使用哪些身份验证令牌、要分叉的工作人员数量、是否缓存呈现的视图、等等。

Your example looks more like a model, so something like a database is probably a better fit.

您的示例看起来更像是一个模型,因此像数据库这样的东西可能更合适。

That said, there's no context around what your app does or how it uses festivals, so if it does turn out that you should use an env var, then you have several options. The simplest is probably to just use a space or comma-delimited string:

也就是说,没有关于您的应用程序做什么或如何使用的上下文festivals,因此如果事实证明您应该使用 env var,那么您有多种选择。最简单的可能是只使用空格或逗号分隔的字符串:

heroku config:set FESTIVALS="bonnaroo lollapalooza coachella"

then:

然后:

var festivals = process.env.FESTIVALS.split(' ');

disclosure: I'm the Node.js Platform Owner at Heroku

披露:我是 Heroku 的 Node.js 平台所有者

回答by Abhishek

Your example looks more of an enumeration than a config array. I'd highly recommend using a model to save it.

您的示例看起来更像是枚举而不是配置数组。我强烈建议使用模型来保存它。

In case you are referring to the above array just as an example and are more curious about how can arrays be stored in an env file -

如果您将上述数组作为示例,并且对如何将数组存储在 env 文件中更加好奇 -

Short answer: You cannot.

简短的回答: 你不能。

Long answer: .env variables are stringsSo something like

长答案: .env 变量是字符串所以类似

BOOLEAN = true

will be treated as

将被视为

BOOLEAN = "true"

and so will

也会

FESTIVALS = ['bonnaroo', 'lollapalooza', 'coachella'] 

be treated as

被视为

FESTIVALS = "['bonnaroo', 'lollapalooza', 'coachella']"

Solution:

解决方案:

You can save the array as a delimited string in .env

您可以将数组保存为 .env 中的分隔字符串

FESTIVALS = "bonnaroo, lollapalooza, coachella"

In your js file you can convert it to an array using

在您的 js 文件中,您可以使用

var festivals = process.env.FESTIVALS.split(",");

回答by mscdex

It probably depends on your data. For example, if none of the values will ever contain commas, you could just make it a comma-separated list and then split on a comma (e.g. starting your app with FOO=bar,baz,quux node myapp.jsthen doing var foo = process.env.FOO.split(',')in myapp.js).

这可能取决于您的数据。例如,如果没有任何值将包含逗号,您可以将其设为逗号分隔的列表,然后在逗号上拆分(例如,使用FOO=bar,baz,quux node myapp.jsthen do var foo = process.env.FOO.split(',')in启动您的应用程序myapp.js)。

Otherwise if your input values can be more complex, JSON will probably be the easiest to work with.

否则,如果您的输入值可能更复杂,那么 JSON 可能是最容易使用的。