0
0
Node.jsframework~30 mins

Environment configuration in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Environment Configuration in Node.js
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Assign the function call result to connectionMessage and use console.log to display it.