In Node.js, environment variables are accessed via the global object process.env.
Step 2: Match the correct syntax
The correct way to get DB_HOST is process.env.DB_HOST. Other options are invalid or from other languages.
Final Answer:
process.env.DB_HOST -> Option A
Quick Check:
Node.js env var = process.env.VAR [OK]
Hint: Node.js env vars use process.env.VAR_NAME [OK]
Common Mistakes:
Using function calls like getEnv() which don't exist
Confusing syntax with other languages like Java
Trying to access env vars without process.env
3. Given this Python snippet in a microservice startup:
import os
env = os.getenv('ENVIRONMENT', 'development')
if env == 'production':
db_url = os.getenv('PROD_DB_URL')
else:
db_url = os.getenv('DEV_DB_URL')
print(db_url)
What will be printed if ENVIRONMENT is not set and DEV_DB_URL is set to "localhost:5432/dev"?
medium
A. "localhost:5432/prod"
B. None
C. "localhost:5432/dev"
D. Error: ENVIRONMENT not set
Solution
Step 1: Check default environment value
The code uses os.getenv('ENVIRONMENT', 'development'), so if ENVIRONMENT is missing, it defaults to 'development'.
Step 2: Determine which DB URL is selected
Since env is 'development', the else branch runs, setting db_url to os.getenv('DEV_DB_URL'), which is "localhost:5432/dev".
Final Answer:
"localhost:5432/dev" -> Option C
Quick Check:
Default env 'development' selects DEV_DB_URL [OK]
Hint: Default env triggers DEV_DB_URL print [OK]
Common Mistakes:
Assuming ENVIRONMENT must be set or error occurs
Confusing production and development branches
Expecting None if variable missing
4. A microservice fails to load its configuration from environment variables and crashes. Which is the most likely cause?
medium
A. The service code has a syntax error unrelated to config
B. Environment variables were not set before service startup
C. The service uses hardcoded values instead of env vars
D. The database is down
Solution
Step 1: Understand environment variable loading
Microservices read environment variables at startup. If variables are missing, config loading fails.
Step 2: Identify cause of crash
If env vars are not set before starting, the service cannot find needed config and may crash or error out.
Final Answer:
Environment variables were not set before service startup -> Option B
Quick Check:
Missing env vars cause config load failure [OK]
Hint: Set env vars before starting service [OK]
Common Mistakes:
Blaming unrelated syntax errors
Assuming hardcoded values cause crashes
Confusing database issues with config loading
5. You have a microservice deployed in three environments: development, staging, and production. You want to use environment-based configuration to manage database URLs securely and avoid code changes. Which approach is best?
hard
A. Use environment variables for DB URLs and load them at startup
B. Store all DB URLs in code and comment/uncomment per environment
C. Use a single DB URL for all environments to simplify config
D. Hardcode production DB URL and pass dev/staging URLs as query params
Solution
Step 1: Evaluate configuration management options
Hardcoding or commenting code per environment is error-prone and unsafe. Using a single DB URL ignores environment differences.
Step 2: Choose secure, flexible best practice
Using environment variables allows each environment to have its own DB URL securely without code changes, loaded at service startup.
Final Answer:
Use environment variables for DB URLs and load them at startup -> Option A
Quick Check:
Env vars for config = secure + flexible [OK]
Hint: Env vars separate config from code for all environments [OK]
Common Mistakes:
Hardcoding config in code for each environment
Using same DB URL everywhere ignoring environment needs