Bird
Raised Fist0
Expressframework~20 mins

Environment-based configuration in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does this Express app behave with NODE_ENV set to 'production'?
Consider this Express app code snippet. What will be the response when NODE_ENV is set to 'production' and a GET request is made to '/'?
Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  if (process.env.NODE_ENV === 'production') {
    res.send('Production mode');
  } else {
    res.send('Development mode');
  }
});

module.exports = app;
AThe server throws an error because NODE_ENV is undefined.
BThe server responds with 'Development mode'.
CThe server responds with 'Production mode'.
DThe server responds with an empty string.
Attempts:
2 left
💡 Hint
Check the condition that compares process.env.NODE_ENV to 'production'.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this environment config setup
Which option contains the correct syntax to load environment variables from a .env file using the dotenv package in an Express app?
Express
require('dotenv').config()

const port = process.env.PORT || 3000;
Aimport dotenv from 'dotenv'; dotenv.config();
Brequire('dotenv').config();
Crequire('dotenv').config
Ddotenv.config()
Attempts:
2 left
💡 Hint
Remember to call the config function properly.
❓ state_output
advanced
2:00remaining
What is the value of 'dbConnectionString' after running this code?
Given this Express configuration snippet, what will be the value of dbConnectionString if NODE_ENV is 'test' and no TEST_DB_URL is set?
Express
const config = {
  development: { dbConnectionString: 'mongodb://localhost/devdb' },
  test: { dbConnectionString: process.env.TEST_DB_URL || 'mongodb://localhost/testdb' },
  production: { dbConnectionString: process.env.PROD_DB_URL }
};

const env = process.env.NODE_ENV || 'development';
const dbConnectionString = config[env].dbConnectionString;
Aprocess.env.TEST_DB_URL
Bundefined
C'mongodb://localhost/devdb'
D'mongodb://localhost/testdb'
Attempts:
2 left
💡 Hint
Look at the fallback value when TEST_DB_URL is not set.
🔧 Debug
advanced
2:00remaining
Why does this Express app always run in production mode?
This Express app always logs 'Running in production mode' even when NODE_ENV is set to 'development'. What is the bug?
Express
const express = require('express');
const app = express();

const env = process.env.NODE_ENV || 'development';

if (env = 'production') {
  console.log('Running in production mode');
} else {
  console.log('Running in development mode');
}

module.exports = app;
AThe if condition uses assignment '=' instead of comparison '==='. It always sets env to 'production' and evaluates as truthy.
Bprocess.env.NODE_ENV is not set, so env is always 'development'.
CThe console.log statements are reversed.
DThe app does not listen on any port.
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
🧠 Conceptual
expert
2:00remaining
Which option best explains why environment-based configuration is important in Express apps?
Why do Express apps use environment-based configuration like NODE_ENV and .env files?
ATo separate settings like database URLs and API keys for development, testing, and production without changing code.
BTo make the app run faster by caching environment variables.
CTo allow users to change the app's behavior by editing the source code directly.
DTo force the app to always use the same configuration regardless of where it runs.
Attempts:
2 left
💡 Hint
Think about why different environments need different settings.

Practice

(1/5)
1. What is the main purpose of using environment-based configuration in an Express app?
easy
A. To change app settings without modifying the code
B. To write all configuration directly inside the code
C. To make the app run only on one specific machine
D. To avoid using any external files or variables

Solution

  1. Step 1: Understand environment-based configuration

    It allows changing settings like ports or database URLs without changing the app code.
  2. Step 2: Compare options

    Only To change app settings without modifying the code describes this purpose correctly; others contradict the idea of flexible configuration.
  3. Final Answer:

    To change app settings without modifying the code -> Option A
  4. Quick Check:

    Environment config = change settings without code [OK]
Hint: Think: config changes without code edits [OK]
Common Mistakes:
  • Confusing environment config with hardcoded settings
  • Thinking environment config restricts app to one machine
  • Assuming environment config means no external variables
2. Which of the following is the correct way to access an environment variable named PORT in an Express app?
easy
A. const port = process.env.PORT;
B. const port = env.PORT;
C. const port = process.PORT;
D. const port = process.env['port'];

Solution

  1. Step 1: Recall how to access environment variables in Node.js

    Use process.env.VARIABLE_NAME to get environment variables.
  2. Step 2: Check each option

    const port = process.env.PORT; uses the correct syntax. const port = env.PORT; misses process. const port = process.PORT; uses wrong property. const port = process.env['port']; uses lowercase key which is case-sensitive and usually environment variables are uppercase.
  3. Final Answer:

    const port = process.env.PORT; -> Option A
  4. Quick Check:

    Use process.env.VAR to access env variables [OK]
Hint: Always use process.env.VAR to get env variables [OK]
Common Mistakes:
  • Omitting 'process' before 'env'
  • Using wrong casing for environment variable names
  • Accessing environment variables as properties of process
3. Consider this Express code snippet:
const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

If the environment variable PORT is set to 5000, what will be printed when the server starts?
medium
A. Server running on port process.env.PORT
B. Server running on port 3000
C. Server running on port undefined
D. Server running on port 5000

Solution

  1. Step 1: Understand the fallback logic

    The code uses process.env.PORT || 3000, so if PORT is set, it uses that; otherwise, 3000.
  2. Step 2: Apply given environment variable

    Since PORT is set to 5000, the server listens on port 5000 and prints that.
  3. Final Answer:

    Server running on port 5000 -> Option D
  4. Quick Check:

    Env var set? Use it; else default [OK]
Hint: If env var exists, use it; else fallback [OK]
Common Mistakes:
  • Ignoring the environment variable and using default
  • Printing the variable name instead of its value
  • Assuming undefined if environment variable exists
4. You wrote this code to read a secret key:
const secret = process.env.SECRET_KEY;
console.log(secret.length);

But when you run the app, it crashes with TypeError: Cannot read property 'length' of undefined. What is the likely cause?
medium
A. You must use process.SECRET_KEY instead of process.env.SECRET_KEY
B. The secret key is empty string, so length is undefined
C. The environment variable SECRET_KEY is not set
D. You need to convert secret to a string before accessing length

Solution

  1. Step 1: Analyze the error message

    The error says length is accessed on undefined, meaning secret is undefined.
  2. Step 2: Check environment variable usage

    If SECRET_KEY is not set in environment, process.env.SECRET_KEY is undefined, causing the error.
  3. Final Answer:

    The environment variable SECRET_KEY is not set -> Option C
  4. Quick Check:

    Undefined env var causes undefined value [OK]
Hint: Check if env variable is set before using it [OK]
Common Mistakes:
  • Assuming env variable is always defined
  • Using wrong property to access env variables
  • Not handling undefined values before property access
5. You want to configure your Express app to use different database URLs for development and production environments. Which approach correctly uses environment variables and safe defaults?
hard
A. const dbUrl = process.env.DB_URL && 'mongodb://localhost/devdb';
B. const dbUrl = process.env.DB_URL || 'mongodb://localhost/devdb';
C. const dbUrl = 'mongodb://localhost/devdb'; if (process.env.NODE_ENV === 'production') dbUrl = process.env.DB_URL;
D. const dbUrl = process.env.DB_URL ? 'mongodb://localhost/devdb' : process.env.DB_URL;

Solution

  1. Step 1: Understand environment-based config with fallback

    Use environment variable if set; otherwise, use a safe default URL.
  2. Step 2: Evaluate each option

    const dbUrl = process.env.DB_URL || 'mongodb://localhost/devdb'; correctly uses process.env.DB_URL || default. const dbUrl = 'mongodb://localhost/devdb'; if (process.env.NODE_ENV === 'production') dbUrl = process.env.DB_URL; tries to reassign a const variable (error). const dbUrl = process.env.DB_URL && 'mongodb://localhost/devdb'; uses && which returns false if DB_URL is missing. const dbUrl = process.env.DB_URL ? 'mongodb://localhost/devdb' : process.env.DB_URL; reverses the condition, causing wrong assignment.
  3. Final Answer:

    const dbUrl = process.env.DB_URL || 'mongodb://localhost/devdb'; -> Option B
  4. Quick Check:

    Use env var or fallback default [OK]
Hint: Use env var OR fallback default with || [OK]
Common Mistakes:
  • Reassigning const variables
  • Using && instead of || for fallback
  • Swapping condition logic causing wrong URL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard