From 9d0dfd046ca1c61d6956032ec922ddfc175212b5 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Sat, 30 Apr 2022 13:30:04 +0100 Subject: [PATCH] Last Sync: 2022-04-30 13:30:04 --- .../NodeJS/Configuring applications.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Programming_Languages/NodeJS/Configuring applications.md diff --git a/Programming_Languages/NodeJS/Configuring applications.md b/Programming_Languages/NodeJS/Configuring applications.md new file mode 100644 index 0000000..28f277f --- /dev/null +++ b/Programming_Languages/NodeJS/Configuring applications.md @@ -0,0 +1,64 @@ +--- +tags: + - Programming_Languages + - backend + - node-js +--- +# Configuring applications + +We will want to run different processes in the different [environments](./Environments.md) 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](https://github.com/node-config/node-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 +```json + // default.json + { + "name": "My Express app" + } + +``` + +## Referencing config files + +```js +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](../Shell_Scripting/Environmental_and_shell_variables.md). + +We create a file called `custom-environment-variables` (must be called this) and map a property to an environmental environment we have already set. + +Let's create an environmental variable for a password: +```bash +export APP_PASSWORD='mypassword123' +``` + +Then in our custom variable file: + +```json +{ + "password": "APP_PASSWORD" +} + +``` \ No newline at end of file