Complete the code to load environment variables in a microservice.
const config = process.env.[1];The NODE_ENV variable is commonly used to determine the environment (development, production, etc.) in microservices.
Complete the code to read a configuration file path from environment variables.
const configPath = process.env.[1] || './config/default.json';
CONFIG_PATH is a common environment variable name used to specify the path to a configuration file.
Fix the error in the code to correctly parse environment variables for service port.
const port = parseInt(process.env.[1], 10) || 3000;
PORT is the standard environment variable name for service port in microservices.
Fill both blanks to create a dictionary comprehension that filters environment variables starting with 'APP_'.
const appEnvVars = Object.fromEntries(Object.entries(process.env).filter(([key, value]) => key.[1]('APP_') === [2]));
Use indexOf('APP_') === 0 which returns true if the key starts with 'APP_'. indexOf returns 0 if the substring starts at the beginning of the string. Alternatively, startsWith('APP_') returns a boolean directly.
Fill all three blanks to create a function that loads environment variables with defaults.
function getEnvVar(name, defaultValue) {
return process.env[[1]] ?? [2] ?? [3];
}The function returns the environment variable by name. If undefined, it returns defaultValue. If that is also undefined or null, it returns an empty string '' as a fallback.