Challenge - 5 Problems
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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;
Attempts:
2 left
💡 Hint
Check the condition that compares process.env.NODE_ENV to 'production'.
✗ Incorrect
When NODE_ENV is set to 'production', the condition is true, so the response is 'Production mode'.
📝 Syntax
intermediate2: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;
Attempts:
2 left
💡 Hint
Remember to call the config function properly.
✗ Incorrect
Option B correctly requires dotenv and calls config() as a function with parentheses and a semicolon.
❓ state_output
advanced2: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;Attempts:
2 left
💡 Hint
Look at the fallback value when TEST_DB_URL is not set.
✗ Incorrect
Since TEST_DB_URL is undefined, the fallback 'mongodb://localhost/testdb' is used.
🔧 Debug
advanced2: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;
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
✗ Incorrect
Using '=' assigns 'production' to env, so the condition is always true, causing the else block never to run.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about why different environments need different settings.
✗ Incorrect
Environment-based configuration helps keep sensitive or environment-specific data outside the code and allows easy switching between setups.