Last Sync: 2022-07-12 19:00:04
This commit is contained in:
parent
6e42a8ada4
commit
5f4bb25404
5 changed files with 94 additions and 110 deletions
|
@ -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'))
|
||||
```
|
|
@ -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
|
||||
```
|
|
@ -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.
|
||||
|
||||
<p style="color:red">! How to keep Express and Node environment in sync?</p>
|
||||
|
||||
## 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'))
|
||||
```
|
||||
|
||||
<p style="color:red">! But how would this be achieved in a production server></p>
|
||||
|
||||
|
||||
<p style="color:red">! And how could we do this programmatically at the start of a local development session without manually setting each environment variable in the terminal?</p>
|
Loading…
Add table
Reference in a new issue