Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thisissoon/node-simple-config
Simple file based configuration with environment variable overrides.
https://github.com/thisissoon/node-simple-config
configuration environment json nodejs toml
Last synced: about 2 months ago
JSON representation
Simple file based configuration with environment variable overrides.
- Host: GitHub
- URL: https://github.com/thisissoon/node-simple-config
- Owner: thisissoon
- Created: 2018-02-27T11:38:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-27T15:56:41.000Z (almost 7 years ago)
- Last Synced: 2024-11-10T02:14:09.824Z (3 months ago)
- Topics: configuration, environment, json, nodejs, toml
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple Config
[](https://travis-ci.org/thisissoon/node-simple-config)
Simple file based configuration with environment variable overrides.
## Quick Start
This example uses [toml](https://github.com/toml-lang/toml) format, but
configurations can be in other [file formats](#file-formats).### Install and create config file
```sh
$ npm install https://github.com/thisissoon/node-simple-config
$ npm install toml
$ vi config.toml
``````toml
[http]
host = "0.0.0.0"
port = 4000
```### Use config in your code
```js
const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');myConfig.get('http.host'); // 0.0.0.0
myConfig.get('http.port'); // 4000myConfig.has('http.port'); // true
```## Environment Overrides
Environment variables can be used to override config values from files.
**Bind individual config keys to env vars**
```sh
MYAPP_HTTP_PORT=5000
```
```js
const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.envPrefix = 'MYAPP';config.bindEnv('http.port');
myConfig.get('http.port'); // 5000
````http.port` is bound to the env var `MYAPP_HTTP_PORT`.
**Automatically bind matching env vars**
```sh
MYAPP_HTTP_PORT=5000
MYAPP_HTTP_HOST=127.0.0.1
```
```js
const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.envPrefix = 'MYAPP';config.autoEnv();
myConfig.get('http.port'); // 5000
myConfig.get('http.host'); // 127.0.0.1
```**The prefix and the path replacer can be configured:**
```sh
AWESOME-HTTP-PORT=5000
```
```js
myConfig.envPrefix = 'AWESOME';
myConfig.envKeyReplacer = '-';
```## Defaults
An application should set sane default values if a config option is
not provided.```js
const Config = require('simple-config');
const myConfig = Config.fromFile('config.toml', 'toml');
myConfig.setDefault('http.host', '127.0.0.1');
myConfig.setDefault('http.port', 5000);
myConfig.setDefault('http.root', '/api');myConfig.get('http.host'); // 0.0.0.0
myConfig.get('http.port'); // 4000
myConfig.get('http.root'); // /api
```## File formats
### JSON
```js
const myConfig = Config.fromFile('config.json');
```### TOML
The toml parser is not included in the dependencies as its optional,
you'll need to install it if you're using toml.```
$ npm install toml
```
```js
const myConfig = Config.fromFile('config.toml', 'toml');
```