📖 Scenario: You are building a simple Deno application that needs to manage configuration settings for connecting to a database. These settings include the database host, port, username, and password.Managing configuration properly helps keep your code clean and makes it easy to change settings without touching the main logic.
🎯 Goal: Create a configuration object in Deno, add a helper variable for the connection string, use the configuration to build a connection URL, and export the final configuration for use in other parts of the app.
📋 What You'll Learn
Create a configuration object with exact keys and values
Add a helper variable to build a connection string
Use the configuration object to create a full database URL
Export the final configuration object
💡 Why This Matters
🌍 Real World
Managing configuration settings is essential in real applications to keep sensitive data like passwords separate and to easily change settings without modifying core logic.
💼 Career
Understanding configuration management is important for backend developers, DevOps engineers, and anyone working with server-side applications or deployment pipelines.
Progress0 / 4 steps
1
Create the configuration object
Create a constant called config that is an object with these exact properties and values: host set to "localhost", port set to 5432, username set to "admin", and password set to "secret".
Deno
Hint
Use const config = {} and add the properties inside the curly braces.
2
Add a helper variable for the connection string
Add a constant called connectionString that combines config.host and config.port into a string formatted as "host:port" using a template literal.
Deno
Hint
Use backticks ` and ${} to insert variables inside the string.
3
Create the full database URL
Create a constant called databaseUrl that uses a template literal to combine config.username, config.password, and connectionString into the format "postgres://username:password@host:port".
Deno
Hint
Use a template literal with all parts combined in the correct order.
4
Export the final configuration
Export a constant called finalConfig that is an object containing databaseUrl and the original config object.
Deno
Hint
Use export const finalConfig = { databaseUrl, config }; to export the object.
Practice
(1/5)
1. What is the main purpose of configuration management in a Deno application?
easy
A. To separate settings like API keys from the main code for flexibility and security
B. To write all code and settings in a single file for simplicity
C. To hardcode secret keys directly in the source code
D. To avoid using environment variables altogether
Solution
Step 1: Understand configuration management role
Configuration management separates settings from code to keep apps flexible and secure.
Step 2: Identify correct practice in Deno
Using environment variables or .env files keeps secrets out of code and allows easy changes.
Final Answer:
To separate settings like API keys from the main code for flexibility and security -> Option A
Quick Check:
Config management = separate settings [OK]
Hint: Remember: keep secrets out of code, use config files [OK]
Common Mistakes:
Hardcoding secrets in source code
Mixing code and settings in one file
Ignoring environment-specific configs
2. Which of the following is the correct way to load environment variables from a .env file in Deno?
easy
A. require('dotenv').config();
B. import { config } from "https://deno.land/std/dotenv/mod.ts"; const env = config();
C. import dotenv from "dotenv"; dotenv.load();
D. import { loadEnv } from "deno_env"; loadEnv();
Solution
Step 1: Identify Deno's dotenv import syntax
Deno uses ES module imports from URLs, like importing config from std/dotenv.
Step 2: Check correct usage
Calling config() loads environment variables from .env file correctly in Deno.
Final Answer:
import { config } from "https://deno.land/std/dotenv/mod.ts"; const env = config(); -> Option B
B. API_SECRET is not a valid environment variable name
C. config is assigned without calling it as a function
D. console.log syntax is incorrect
Solution
Step 1: Check how config is used
config is a function and must be called with parentheses to load env variables.
Step 2: Identify the error
Assigning config without () means env is the function itself, not the loaded object.
Final Answer:
config is assigned without calling it as a function -> Option C
Quick Check:
Call config() to load env [OK]
Hint: Always call config() with parentheses [OK]
Common Mistakes:
Assigning function reference instead of calling it
Misspelling config import
Using wrong variable names
5. You want to manage different configurations for development and production in a Deno app. Which approach is best practice?
hard
A. Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable
B. Hardcode production secrets in the code and use .env only for development
C. Use a single .env file with all keys and comment out unused ones manually
D. Store all secrets in a JSON file checked into version control
Solution
Step 1: Understand environment-specific configs
Different environments need different settings to avoid mistakes and leaks.
Step 2: Choose best practice for Deno
Using separate .env files and loading based on an environment variable keeps configs clean and secure.
Final Answer:
Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable -> Option A
Quick Check:
Separate env files per environment = Use separate .env files like .env.development and .env.production and load the correct one based on an environment variable [OK]
Hint: Use multiple .env files and switch by env variable [OK]
Common Mistakes:
Hardcoding secrets in code
Using one .env for all environments
Checking secrets into version control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.