0
0
NestJSframework~20 mins

Custom configuration files in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS config service usage?
Given a custom configuration file that exports a function returning an object, what will be the value of configService.get('database.host') after the module loads?
NestJS
import { ConfigModule, ConfigService } from '@nestjs/config';

// config/database.config.ts
export default () => ({
  database: {
    host: 'localhost',
    port: 5432
  }
});

// app.module.ts
import { Module } from '@nestjs/common';
@Module({
  imports: [ConfigModule.forRoot({ load: [require('./config/database.config').default] })],
})
export class AppModule {}

// some.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class SomeService {
  constructor(private configService: ConfigService) {}

  getDbHost() {
    return this.configService.get('database.host');
  }
}
A'localhost'
Bundefined
C'database.host'
Dnull
Attempts:
2 left
💡 Hint
Remember that the config loader function returns an object with nested keys.
📝 Syntax
intermediate
2:00remaining
Which option correctly imports and uses a custom config file in NestJS?
You want to load a custom config file named app.config.ts that exports a default function returning an object. Which import and usage in ConfigModule.forRoot is correct?
AConfigModule.forRoot({ load: [import('./config/app.config')] })
BConfigModule.forRoot({ load: [require('./config/app.config').default] })
CConfigModule.forRoot({ load: ['./config/app.config'] })
DConfigModule.forRoot({ load: [() => import('./config/app.config')] })
Attempts:
2 left
💡 Hint
The load option expects an array of functions returning config objects.
🔧 Debug
advanced
2:00remaining
Why does this custom config file cause a runtime error in NestJS?
Consider this custom config file settings.config.ts:
export default () => {
  return {
    apiKey: process.env.API_KEY,
    timeout: parseInt(process.env.TIMEOUT)
  }
}
When running the app, it crashes with TypeError: Cannot read property 'API_KEY' of undefined. What is the cause?
AThe config function must return a Promise but returns an object
BparseInt is used incorrectly causing a runtime error
Cprocess.env is undefined because environment variables are not loaded before config runs
DThe export default syntax is invalid in TypeScript
Attempts:
2 left
💡 Hint
Check if environment variables are available when the config function runs.
state_output
advanced
2:00remaining
What is the value of configService.get('feature.enabled') after this config loads?
Given this custom config file feature.config.ts:
export default () => ({
  feature: {
    enabled: process.env.FEATURE_ENABLED === 'true'
  }
});
And the environment variable FEATURE_ENABLED is not set, what does configService.get('feature.enabled') return?
Afalse
Bundefined
Ctrue
Dnull
Attempts:
2 left
💡 Hint
What is the result of comparing undefined to 'true' with ===?
🧠 Conceptual
expert
2:00remaining
Which statement about NestJS custom configuration files is TRUE?
Select the correct statement about how custom configuration files work in NestJS ConfigModule.
ACustom config files must be JSON files; ConfigModule cannot load TypeScript files directly.
BCustom config files can export any value type; ConfigModule uses only the first loaded config file.
CConfigModule requires custom config files to export classes with static methods for config values.
DCustom config files must export a function returning a plain object; ConfigModule merges all loaded configs into one object.
Attempts:
2 left
💡 Hint
Think about how multiple config files combine in NestJS.