Getting Started - ameliazz/mozz.env GitHub Wiki
Basic usage
Setup
First create the mozz.profile.json
file and define which environments the application will have like this:
{
"environments": {
"development": {
"server_hostname": "localhost",
"server_port": 8000
},
"production": {
"server_hostname": "0.0.0.0",
"sever_port": "$env('PORT', 3000)"
}
}
}
You can look the documentation of
mozz.profile.json
, here.
In this case, we created two environments called "development" and "production" for a supposed API, we also defined two variables:
server_hostname
server_port
server_hostname
is normal, right? Just a static value that changes depending on the environment. server_port
is a bit strange? Well, this is one of the reasons why Mozz is used, you can try utility functions like $env()
(view here) which is used to get a value from the loaded env file, at this case, PORT
variable.
Now, we will create the .env
and tell it which environment the application will use when it starts:
MOZZ_ENV="development"
You can generate types with Mozz CLI, the
mzz
. Just usemzz generate
in your shell.
Instantiating
We can just import and create a instance:
import Mozz from 'mozz.env'
const Enhancer = new Mozz()
Using
To access the port defined in the environments, we must do the following way
Enhancer.env.server_hostname // localhost
Enhancer.env.server_port // 8000