Nodejs 环境下.env配置环境变量(附源码分析)

背景介绍

在使用阿里云的函数计算时,使用了其一键部署工具fun, fun 在使用时需要配置 ACCOUNT_ID、REGION、ACCESS_KEY_ID、ACCESS_KEY_SECRET=xxxxxxxxxx等信息。 在使用时直接在根目录下创建.env文件,并写入
ACCOUNT_ID=xxxxxxxx
REGION=cn-shanghai
ACCESS_KEY_ID=xxxxxxxxxxxx
ACCESS_KEY_SECRET=xxxxxxxxxx 即可直接完成配置。
出于好奇,对.env文件的使用稍作了了解。整理如下:

一、.env的作用

.env文件是用来自定义配置的一个简单方法,可以将一些不能在代码中存储的敏感/账号数据从代码中剥离出来,作为环境变量存储在环境中。

二、.env的使用方法

.env 文件通常不包含在版本控制内,它可能包含敏感的 API Key 或者 密码。所有需要环境变量定义(不敏感的定义)的项目都需要创建一个.env.example 文件,这个环境变量包含他们自己定义的环境变量或者联合开发包含的环境变量。项目合作开发者可以独立的复制 .env.example并且重命名为.env,并且修改为正确的本地环境配置,存储密码key或者提供他们必要的值。 在这个使用方法中 .env 文件应该添加到.gitignore文件中并且永远不会被项目的合作者签入/签出。这个方法确保里边没有敏感的 API Key 或者 密码在版本控制中出现从而减少了安全风险,并且开发环境中的配置永远不会告知合作开发者。

  1. 在根目录下添加.env文件
    eg:
DB_HOST=127.0.0.1
DB_NAME=timeseriesmonitor"
DB_PORT=5432
DB_USER=tsm
DB_UNSECURE=true
  1. 引入dotenv
    npm install dotenv
let dotenv = require('dotenv');

dotenv.config('./env');
console.log(process.env);

打印log如下:

{
  ...
  DB_HOST: '127.0.0.1',
  DB_NAME: 'timeseriesmonitor',
  DB_PORT: '5432',
  DB_UNSECURE: 'true',
  DB_USER: 'tsm',
  ...
}

三、 关于dotenv

dotenv是一个NPM包,其作用为将.env文件解析为json对象,并对其中的key-value对通过process.env将其赋值为环境变量。之后便可通过process.env[key],eg: process.env.DB_HOST获取环境变量。

附dotenv源码: dotenv/main.js

const fs = require('fs')
const path = require('path')

function log (message /*: string */) {
  console.log(`[dotenv][DEBUG] ${message}`)
}

// Parses src into an Object
function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {
  const debug = Boolean(options && options.debug)
  const obj = {}

  // convert Buffers before splitting into lines and processing
  src.toString().split('\n').forEach(function (line, idx) {
    // matching "KEY' and 'VAL' in 'KEY=VAL'
    const keyValueArr = line.match(/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/)
    // matched?
    if (keyValueArr != null) {
      const key = keyValueArr[1]

      // default undefined or missing values to empty string
      let value = keyValueArr[2] || ''

      // expand newlines in quoted values
      const len = value ? value.length : 0
      if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
        value = value.replace(/\\n/gm, '\n')
      }

      // remove any surrounding quotes and extra spaces
      value = value.replace(/(^['"]|['"]$)/g, '').trim()

      obj[key] = value
    } else if (debug) {
      log(`did not match key and value when parsing line ${idx + 1}: ${line}`)
    }
  })

  return obj
}

// Populates process.env from .env file
function config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */ {
  let dotenvPath = path.resolve(process.cwd(), '.env')
  let encoding /*: string */ = 'utf8'
  let debug = false

  if (options) {
    if (options.path != null) {
      dotenvPath = options.path
    }
    if (options.encoding != null) {
      encoding = options.encoding
    }
    if (options.debug != null) {
      debug = true
    }
  }

  try {
    // specifying an encoding returns a string instead of a buffer
    const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug })

    Object.keys(parsed).forEach(function (key) {
      if (!process.env.hasOwnProperty(key)) {
        process.env[key] = parsed[key]
      } else if (debug) {
        log(`"${key}" is already defined in \`process.env\` and will not be overwritten`)
      }
    })

    return { parsed }
  } catch (e) {
    return { error: e }
  }
}

module.exports.config = config
module.exports.load = config
module.exports.parse = parse

参考:

  1. [译] dotenv - PHP版本的 .env
  2. Github地址: https://github.com/motdotla/dotenv
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章