【问题标题】:Using Variables in my .env File在我的 .env 文件中使用变量
【发布时间】:2018-08-06 15:14:27
【问题描述】:

请知道如何在我的 env 文件中设置变量?

Website_Base_URL=https://${websiteId}.dev.net/api

在我的代码中:

websiteId = 55;

我想打电话给config.get('Website_Base_URL');,所以它会返回完整的网址:

https://55.dev.net/api

在我的 default.js 我有

Website_Base_URL: process.env.Website_Base_URL,

【问题讨论】:

  • 尝试设置环境变量的哪一部分?
  • dotenv
  • 我想调用 config.get('Website_Base_URL');并将 websiteId 作为参数传递,因此它返回参数 + baseUrl

标签: javascript node.js environment-variables


【解决方案1】:

正如@guijob 所说,dotenv 可以使用。您也可以从缓冲区中获取 process.env。

const dotenv = require('dotenv');

const websiteId = 55;
const buf = Buffer.from(`Website_Base_URL=https://${websiteId}.dev.net/api`);
const config = dotenv.parse(buf) // will return an object

/// this below is from dotenv `config()` function

Object.keys(config).forEach(function (key) {
  if (!process.env.hasOwnProperty(key)) {
    process.env[key] = parsed[key]
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多