In this practice, you will set up and work with environment variables in a Node.js environment.
cd
into the server
folder.
npm install
the dependencies.
The index.js
file contains a single console.log
that will log the value of
the NODE_ENV
environment variable; however, it currently prints undefined
when running:
> node index.js
> undefined
Run index.js
again, but this time with the NODE_ENV
environment variable set
to "production" and declared within the command-line.
The app.js
file contains some boilerplate code for an Express server. However,
it is dependent on some environment variables. Identify the environment
variables that the application is using.
Set the environment variables so that the server:
- displays the secret message "Hello from .env" at http://localhost:5000
However, instead of defining the environment variables through the command-line,
use a .env
file to set the environment variables that the server needs.
Create the .env file with the appropriate environment variables and values.
Then, use dotenv
to connect your environment variables so that the Express
application runs with the configured environment variables.
Important Note: You will need to npm install
the appropriate node modules
to use dotenv
.
You learned one way for setting and loading environment variables in a Node.js application and another way for setting and loading environment variables in an Express application.