0
0
Node.jsframework~10 mins

process.env for environment variables in Node.js - Interactive Code Practice

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

Complete the code to access the environment variable named PORT.

Node.js
const port = process.env.[1];
Drag options to blanks, or click blank then click option'
APORT
Bport
CENV_PORT
DprocessPort
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase variable names like 'port' instead of 'PORT'.
Trying to access environment variables without process.env.
2fill in blank
medium

Complete the code to provide a default value of 3000 if the PORT environment variable is not set.

Node.js
const port = process.env.PORT || [1];
Drag options to blanks, or click blank then click option'
A3000
B"default"
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string 'default' instead of a number.
Not providing a fallback value.
3fill in blank
hard

Fix the error in accessing the environment variable named DATABASE_URL.

Node.js
const dbUrl = process.[1].DATABASE_URL;
Drag options to blanks, or click blank then click option'
AENV
Benv
CEnv
Denvironment
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'ENV' instead of 'env'.
Trying to access environment variables directly on process.
4fill in blank
hard

Fill both blanks to safely read the environment variable API_KEY or use 'defaultKey' if not set.

Node.js
const apiKey = process.env.[1] || [2];
Drag options to blanks, or click blank then click option'
AAPI_KEY
B"defaultKey"
C"default"
DAPIKEY
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names like 'APIKEY'.
Not quoting the default string value.
5fill in blank
hard

Fill all three blanks to create an object with keys as environment variable names and values as their values, filtering only variables starting with 'APP_'.

Node.js
const appEnv = Object.fromEntries(Object.entries(process.env).filter(([[1]]) => [2].startsWith([3])));
Drag options to blanks, or click blank then click option'
Akey
C"APP_"
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'key' in the filter.
Not quoting the prefix string.
Mixing up key and value variables.