From 5f4bb2540487231f086a78fe2858e44ef1180022 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Tue, 12 Jul 2022 19:00:04 +0100 Subject: [PATCH] Last Sync: 2022-07-12 19:00:04 --- .../NodeJS/Architecture/Environments.md | 0 .../Environments/Configuring_applications.md | 69 -------------- .../Architecture/Environments/Environments.md | 41 -------- .../Architecture/Managing_environments.md | 94 +++++++++++++++++++ .../Architecture/{Environments => }/Ports.md | 0 5 files changed, 94 insertions(+), 110 deletions(-) delete mode 100644 Programming_Languages/NodeJS/Architecture/Environments.md delete mode 100644 Programming_Languages/NodeJS/Architecture/Environments/Configuring_applications.md delete mode 100644 Programming_Languages/NodeJS/Architecture/Environments/Environments.md create mode 100644 Programming_Languages/NodeJS/Architecture/Managing_environments.md rename Programming_Languages/NodeJS/Architecture/{Environments => }/Ports.md (100%) diff --git a/Programming_Languages/NodeJS/Architecture/Environments.md b/Programming_Languages/NodeJS/Architecture/Environments.md deleted file mode 100644 index e69de29..0000000 diff --git a/Programming_Languages/NodeJS/Architecture/Environments/Configuring_applications.md b/Programming_Languages/NodeJS/Architecture/Environments/Configuring_applications.md deleted file mode 100644 index 9dc0aa5..0000000 --- a/Programming_Languages/NodeJS/Architecture/Environments/Configuring_applications.md +++ /dev/null @@ -1,69 +0,0 @@ ---- -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 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: -```bash -export APP_PASSWORD='mypassword123' -``` - -Then in our custom variable file: - -```json -{ - "password": "APP_PASSWORD" -} - -``` - -We can then safely reference this value in the course of our normal code: -```js -console.log(config.get('password')) -``` \ No newline at end of file diff --git a/Programming_Languages/NodeJS/Architecture/Environments/Environments.md b/Programming_Languages/NodeJS/Architecture/Environments/Environments.md deleted file mode 100644 index cbe8f56..0000000 --- a/Programming_Languages/NodeJS/Architecture/Environments/Environments.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -tags: - - Programming_Languages - - backend - - node-js ---- -# Environments - -With a full-scale Node application you will typically run three environments: -* Development -* Testing -* Production - -### Accessing current Node environment - We can control which processes run in a particular environment via the Node envrionment variables: `process.env` (see for instance [ports](./Ports.md)). - -To determine the current environment we can use the variable `process.env.NODE_ENV`. This works globally regardless of the kind of Node app we are building. In Express, there is a built in method for retrieving the current envrionment: `app.get('env')`. - -If you haven't manually set up your environments Node will return `undefined` but express defaults to `development`. - -```js - -console.log(process.env.NODE_ENV); // undefined -console.log(app.get("env")); // development - -``` - -Here is an example of setting middleware to run only in the specified environment: - -```js -if (app.get("env") === 'development') { - app.use(morgan("common")); - console.log('Morgan enabled') -} -``` -### Setting the current environment -We could test that the previous code block works by switching the environment to production. We would do this by setting the environment variable in the terminal: - -``` -export NODE_ENV=production -``` \ No newline at end of file diff --git a/Programming_Languages/NodeJS/Architecture/Managing_environments.md b/Programming_Languages/NodeJS/Architecture/Managing_environments.md new file mode 100644 index 0000000..b335471 --- /dev/null +++ b/Programming_Languages/NodeJS/Architecture/Managing_environments.md @@ -0,0 +1,94 @@ +--- +tags: + - Programming_Languages + - backend + - node-js +--- + +# Managing environments + +With a full-scale Node application you will typically run three environments: +* Development +* Testing +* Production + +## Accessing the current environment + +To determine the current environment we can use the variable **`process.env.NODE_ENV`**. This works globally regardless of the kind of Node app we are building. + +If you have not manually set up your environments, **`process.env.NODE_ENV`** will return `undefined`. + +### Setting the Node environment +#### For the session +`NODE_ENV` is a bash [environment variable](/Programming_Languages/Shell_Scripting/Environmental_and_shell_variables.md) like any other. So we can set it in the normal way: + +```bash +export NODE_ENV=production +``` + +### In Express +In Express, there is a built in method for retrieving the current envrionment: `app.get('env')`. Express will default to the development environment. + +

! How to keep Express and Node environment in sync?

+ +## Configuring environments + +We use the third-party [Config](https://github.com/node-config/node-config) package to manage different configurations based on the environment. + +Once installed we set up a dedicated config directory with a structure as follows: + +``` +config/ + default.json + development.json + production.json +``` + +For example: + +```json + // default.json + { + "name": "My Express app" + } +``` +Then to utilise config variables: + +```js +const config = require('config') +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). + +### Sensitive config data + +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) alongside the config pacakage. + +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" +} + +``` + +We can then safely reference this value in the course of our normal code: +```js +console.log(config.get('password')) +``` + +

! But how would this be achieved in a production server>

+ + +

! And how could we do this programmatically at the start of a local development session without manually setting each environment variable in the terminal?

\ No newline at end of file diff --git a/Programming_Languages/NodeJS/Architecture/Environments/Ports.md b/Programming_Languages/NodeJS/Architecture/Ports.md similarity index 100% rename from Programming_Languages/NodeJS/Architecture/Environments/Ports.md rename to Programming_Languages/NodeJS/Architecture/Ports.md