0
0
Expressframework~20 mins

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

Choose your learning style9 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.