📖 Scenario: You are building a simple Node.js application that needs to connect to a database. To keep your database connection details safe and flexible, you will use environment variables.
🎯 Goal: Create a Node.js script that reads database connection details from environment variables and logs a connection message using those details.
📋 What You'll Learn
Create environment variables for database host, user, and password
Use a configuration object to hold these variables
Write a function that uses the configuration to simulate a database connection message
Log the connection message using the configuration values
💡 Why This Matters
🌍 Real World
Environment variables keep sensitive data like passwords out of your code and allow easy changes without editing code.
💼 Career
Understanding environment configuration is essential for backend developers to build secure and flexible applications.
Progress0 / 4 steps
1
Set up environment variables
Create three environment variables named DB_HOST, DB_USER, and DB_PASS with the exact values localhost, admin, and secret123 respectively. Use process.env to access these variables in your Node.js script.
Node.js
Hint
Use process.env.VARIABLE_NAME to read environment variables in Node.js.
2
Create a configuration object
Create a constant object named dbConfig that holds the properties host, user, and password using the variables DB_HOST, DB_USER, and DB_PASS.
Node.js
Hint
Create an object with keys host, user, and password assigned to the environment variables.
3
Write a function to simulate connection
Write a function named connectToDatabase that takes the dbConfig object as a parameter and returns a string: Connected to database at HOST with user USER, replacing HOST and USER with the corresponding values from dbConfig.
Node.js
Hint
Use a template string to include config.host and config.user in the returned message.
4
Log the connection message
Call the connectToDatabase function with dbConfig and assign the result to a constant named connectionMessage. Then, log connectionMessage to the console.
Node.js
Hint
Assign the function call result to connectionMessage and use console.log to display it.
Practice
(1/5)
1. What is the main purpose of using environment configuration in a Node.js application?
easy
A. To make the application run faster by hardcoding values
B. To write all configuration directly inside the main JavaScript files
C. To separate settings like API keys and ports from the code for flexibility and security
Step 1: dotenv loads .env variables into process.env
The call to require('dotenv').config() loads PORT=4000 into process.env.PORT.
Step 2: Evaluate console.log expression
The expression process.env.PORT || 3000 uses the loaded value 4000, so it prints 4000.
Final Answer:
4000 -> Option A
Quick Check:
Loaded PORT = 4000 [OK]
Hint: Loaded env vars override defaults [OK]
Common Mistakes:
Assuming default 3000 prints even if PORT is set
Thinking process.env.PORT is undefined
Expecting an error if .env exists
4. What is the error in this Node.js code that tries to use environment variables?
import dotenv from 'dotenv';
dotenv.config;
console.log(process.env.DB_HOST);
medium
A. dotenv package not imported correctly
B. Missing parentheses after config method call
C. process.env.DB_HOST is undefined by default
D. Cannot use import syntax in Node.js
Solution
Step 1: Check dotenv usage
The code calls dotenv.config without parentheses, so the config function is not executed.
Step 2: Effect of missing parentheses
Without calling config(), environment variables from .env are not loaded, so process.env.DB_HOST may be undefined.
Final Answer:
Missing parentheses after config method call -> Option B
Quick Check:
Call config() with () to load env [OK]
Hint: Always call config() with parentheses [OK]
Common Mistakes:
Forgetting () after config
Assuming import syntax always fails
Ignoring that process.env can be empty
5. You want to safely use an API key in your Node.js app without exposing it in your code. Which approach correctly applies environment configuration best practices?
hard
A. Write the API key in a JSON file and require it in your code
B. Hardcode the API key directly in your main JavaScript file for easy access
C. Push the API key to your public GitHub repository for backup
D. Store the API key in a .env file, load it with dotenv, and access it via process.env.API_KEY
Solution
Step 1: Identify secure storage method
Storing secrets like API keys in a .env file keeps them out of code and version control.
Step 2: Use dotenv to load and access keys
Using dotenv loads the key into process.env, which the app can safely use.
Final Answer:
Store the API key in a .env file, load it with dotenv, and access it via process.env.API_KEY -> Option D
Quick Check:
Secrets in .env, not code [OK]
Hint: Keep secrets in .env, never in code [OK]
Common Mistakes:
Hardcoding secrets in code files
Committing secrets to public repos
Using insecure files for secrets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.