简体   繁体   English

Digitalocean的app平台组件环境变量如何使用?

[英]How to use Digitalocean's app platform component environmental variables?

I am using Digital Ocean's app platform to host a NodeJS app.我正在使用 Digital Ocean 的应用平台来托管 NodeJS 应用。 I do not understand the documentation for using environmental variables within my NodeJS code.我不了解在我的 NodeJS 代码中使用环境变量的文档。 https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables when I try to add the environmental variable like I think the documentation wants me to I get a syntax error: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true}); https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables当我尝试添加我认为文档想要的环境变量时我收到一个语法错误: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true}); What is the correct usage?正确的用法是什么?

Step 1:步骤1:

In your App settings you should find the App-Level Environment Variables.在您的应用程序设置中,您应该找到应用程序级环境变量。

You could set a variable there like this:你可以像这样在那里设置一个变量:

Keys: APP_LEVEL_EXAMPLE_VAR 
Values: rafiki

Don´t forget to save.不要忘记保存。

Now you have an App-Level Environment Variable.现在你有了一个应用级环境变量。

Step 2:第2步:

In your app´s project.yml file, you can set a variable that reads the App-Level variable like this:在您的应用程序的 project.yml 文件中,您可以设置一个读取应用程序级变量的变量,如下所示:

environment: { 
  EXAMPLE_VAR: ${APP_LEVEL_EXAMPLE_VAR}
}

Step 3:第 3 步:

Now you can access the value of APP_LEVEL_EXAMPLE_VAR in your app like this:现在您可以像这样在您的应用程序中访问 APP_LEVEL_EXAMPLE_VAR 的值:

const value = process.env.EXAMPLE_VAR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM