eolas/Programming_Languages/NodeJS/Configuring applications.md
2022-04-30 14:00:04 +01:00

1.8 KiB

tags
Programming_Languages
backend
node-js

Configuring applications

We will want to run different processes in the different environments of production, development, and testing. We should specify these in dedicated config files rather than within the main body of our code.

We can use the third-party Config package to assist with this.

We would set up a config directory with the following structure:

config/
    default.json
    development.json
    production.json

Example configurations

    // default.json 
    {
        "name": "My Express app"
    }

Referencing config files

const config = require('config')

// Utilise a config variable:

console.log('Application name:' + config.get('name'))

If we toggled the different environments, we would see different outputs from the above code (assuming we had different config files in /config with different names).

Managing sensitive configuration items safely

We will need to store passwords, API keys and other kinds of authentication data for our application. We obviously shouldn't store this data openly in our config files since it would be made public.

We can do so securely by utilising environmental variables.

We create a file called custom-environment-variables (must be called this to work with the config package) and map a property to an environmental environment we have already set.

Let's create an environmental variable for a password:

export APP_PASSWORD='mypassword123'

Then in our custom variable file:

{
    "password": "APP_PASSWORD"
}

We can then safely reference this value in the course of our normal code:

console.log(config.get('password'))