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

时间:2022-02-23 22:45:24

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字符串作为环境变量?

2 个解决方案

#1


24  

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.

通常,您希望使用环境变量来提供应用程序关于其环境的信息,或者定制它的行为:要连接到哪个数据库、要使用哪个令牌、要叉多少worker、是否缓存已呈现的视图等等。

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:

也就是说,关于你的应用程序做什么或它如何使用节日没有上下文,所以如果它确实是你应该使用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的js平台所有者

#2


2  

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.js then doing var foo = process.env.FOO.split(',') in myapp.js).

这可能取决于你的数据。例如,如果没有一个值会包含逗号,您可以将它设置为逗号分隔的列表,然后在逗号上进行分隔(例如,使用FOO=bar、baz、quux节点myapp启动应用程序。然后在myapp.js中执行var foo = process.env.FOO.split(',')。

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

否则,如果输入值可能更复杂,JSON可能是最容易处理的。

#1


24  

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.

通常,您希望使用环境变量来提供应用程序关于其环境的信息,或者定制它的行为:要连接到哪个数据库、要使用哪个令牌、要叉多少worker、是否缓存已呈现的视图等等。

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:

也就是说,关于你的应用程序做什么或它如何使用节日没有上下文,所以如果它确实是你应该使用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的js平台所有者

#2


2  

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.js then doing var foo = process.env.FOO.split(',') in myapp.js).

这可能取决于你的数据。例如,如果没有一个值会包含逗号,您可以将它设置为逗号分隔的列表,然后在逗号上进行分隔(例如,使用FOO=bar、baz、quux节点myapp启动应用程序。然后在myapp.js中执行var foo = process.env.FOO.split(',')。

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

否则,如果输入值可能更复杂,JSON可能是最容易处理的。