0
0
Expressframework~10 mins

Environment-based configuration in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load environment variables from a .env file.

Express
require('[1]').config();
Drag options to blanks, or click blank then click option'
Aconfig
Bexpress
Cdotenv
Denv
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'dotenv' to load environment variables.
Forgetting to call the config() method after requiring the package.
2fill in blank
medium

Complete the code to access the PORT environment variable with a fallback value.

Express
const port = process.env.[1] || 3000;
Drag options to blanks, or click blank then click option'
AHOST
BPORT
CNODE_ENV
DDATABASE_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using HOST instead of PORT for the server port.
Not providing a fallback value.
3fill in blank
hard

Fix the error in the code to correctly use environment variables in Express.

Express
app.listen([1], () => {
  console.log(`Server running on port ${port}`);
});
Drag options to blanks, or click blank then click option'
Aport
Bprocess.env.PORT
C3000
DPORT
Attempts:
3 left
💡 Hint
Common Mistakes
Passing process.env.PORT directly without fallback.
Passing the string 'PORT' instead of a number.
4fill in blank
hard

Fill both blanks to set a default environment and log it.

Express
const env = process.env.[1] || '[2]';
console.log(`Running in ${env} mode`);
Drag options to blanks, or click blank then click option'
ANODE_ENV
Bproduction
Cdevelopment
DPORT
Attempts:
3 left
💡 Hint
Common Mistakes
Using PORT instead of NODE_ENV for environment mode.
Setting production as default instead of development.
5fill in blank
hard

Fill all three blanks to create a config object using environment variables.

Express
const config = {
  host: process.env.[1] || 'localhost',
  port: process.env.[2] || 5432,
  user: process.env.[3] || 'admin'
};
Drag options to blanks, or click blank then click option'
ADB_HOST
BDB_PORT
CDB_USER
DDATABASE_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using DATABASE_URL instead of separate variables for host, port, and user.
Mixing up the order of variables.