@oerlikon/config
Easily add server and client configuration to your oerlikon app.
This package will provide server and client config that can be changed at runtime with environment variables.
If you use typescript, you will get typings based on your configuration.
Only the configuration under client
will be accessibly on the frontend so your secrets stay secret.
Installation
-
Install the npm package:
npm install @oerlikon/config
-
Create a JSON config file at ./config/default.json
, e.g.
{
"serverSecret": "your-secret",
"client": {
"apiUrl": "your-url"
}
}
This is your default configuration for your app
-
Add the middleware to your express server:
import configMiddleware from '@oerlikon/config/middleware';
...
app.use(configMiddleware);
-
only applies if you use typescript: add typescript include directory:
in ./tsconfig.json
, add 'config'
to include
in ./client/tsconfig.json
, add '../config'
to include
Usage
On the server:
import config from '@oerlikon/config';
console.log(config.serverSecret);
On the client:
import config from '@oerlikon/config/client';
console.log(config.apiUrl);
Advanced usage
set context config
You can place additional config files next to your default.json
file:
development.json
: Config used in developmentproduction.json
: Config used in productiontest.json
: Config used in testing
All config files will extend the default.json
.
overwrite config with environment variables
Place a configuration file name custom-environment-variables.json
next to your default.json
.
It should have the same structure as your other configuration files, but instead of providing values,
you provide a mapping between environment variables and your config:
{
"serverSecret": "SERVER_SECRET",
"client": {
"apiUrl": "API_URL"
}
}