1.0.3 • Published 5 years ago

node-env-read v1.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

node-env-read

安装

(c)npm install node-env-read -D

使用

const EnvConfig = require('node-env-read')
const envConfig = new EnvConfig('env file path')

API

  • constructor
    • EnvConfig
      • param
        • 配置文件路径 \<String>
  • methods
    • get
      • param
        • key <String | Null>
      • return
        • value <String | Object>
    • has
      • param
        • key <String | Null>
      • return
        • has \<Boolean>

例子

  • 基础
# mysql
MYSQL_HOST   =  localhost
MYSQL_PORT   =  3306

# redis
REDIS_HOST   = localhost

#
KEY          = value

# ENV
IS_DEV       = false
ENV          = dev
const path = require('path')
const EnvConfig = require('node-env-read')
const envConfig = new EnvConfig(path.resolve(__dirname, './.env'))
console.log(envConfig.get('IS_DEV'))            // false
console.log(envConfig.has('MYSQL_PORT'))        // true
console.log(envConfig.get('MYSQL_HOST'))        // 'localhost'
console.log(envConfig.get('MYSQL_PORT'))        // 3306
  • 其他

#开头的则认为是注释说明

# 这是注释
HOST = localhost

通过键值分割的是以第一个=为基准

user===long
console.log(envConfig.get('user'))        // ==long

第一位为=将进行忽略,因为这是不标准的配置格式

===long

=结尾并且只有一个=将匹配到''

HOST  =   
console.log(envConfig.get('PORT') === '')        // true

程序会自动识别boolean以及number类型

IS_DEV      = false
PORT        = 3306
console.log(typeof envConfig.get('IS_DEV'))        // boolean
console.log(typeof envConfig.get('PORT'))          // number
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago