What is the purpose of custom configuration files in NestJS?
Custom configuration files in NestJS help organize and manage app settings separately from code, making it easier to change values like database info or API keys without touching the main code.
Click to reveal answer
intermediate
How do you load a custom configuration file in NestJS using @nestjs/config?
You import ConfigModule and use ConfigModule.forRoot({ load: [yourConfigFunction] }) where yourConfigFunction returns an object with your settings.
Click to reveal answer
beginner
What format do custom configuration files usually use in NestJS?
They usually export a function that returns a plain JavaScript object with key-value pairs representing configuration settings.
Click to reveal answer
beginner
How can you access a custom configuration value inside a NestJS service?
Inject ConfigService in the service constructor, then call configService.get('keyName') to get the value from your custom config.
Click to reveal answer
beginner
Why is it good practice to separate configuration into custom files in NestJS?
Separating config helps keep code clean, makes it easy to change settings per environment, and improves security by isolating sensitive info.
Click to reveal answer
Which NestJS module is used to load custom configuration files?
AConfigModule
BHttpModule
CDatabaseModule
DAppModule
✗ Incorrect
ConfigModule is the official NestJS module to manage configuration, including loading custom config files.
How do you provide a custom config file to ConfigModule?
AUsing forFeature()
BUsing forRoot({ load: [configFunction] })
CBy importing the file directly in main.ts
DBy setting environment variables only
✗ Incorrect
You pass an array of functions returning config objects to the load option in forRoot.
What does a custom config file export in NestJS?
AA JSON string
BA class extending ConfigService
CA function returning an object with config keys and values
DA middleware function
✗ Incorrect
Custom config files export a function that returns a plain object with configuration.
How do you access a config value inside a service?
AInject ConfigService and call get('key')
BImport the config file directly
CUse process.env only
DCall a global variable
✗ Incorrect
ConfigService provides a get method to safely access config values.
Why separate config into custom files?
ATo make config harder to find
BTo increase app size
CTo avoid using environment variables
DTo keep code clean and manage settings easily
✗ Incorrect
Separating config helps maintainability and flexibility.
Explain how to create and use a custom configuration file in a NestJS project.
Think about how NestJS loads and accesses config.
You got /3 concepts.
Why is using custom configuration files beneficial in NestJS applications?
Consider maintainability and security.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using custom configuration files in a NestJS application?
easy
A. To write all logic inside configuration files
B. To keep app settings organized and separate from the main code
C. To replace the main app module
D. To store user data permanently
Solution
Step 1: Understand configuration role
Custom configuration files are used to keep settings like API keys or database info separate from the main code.
Step 2: Identify the correct purpose
This separation helps organize the app and makes it easier to change settings without touching code logic.
Final Answer:
To keep app settings organized and separate from the main code -> Option B
Quick Check:
Custom config files organize settings [OK]
Hint: Custom configs separate settings from code for clarity [OK]
Common Mistakes:
Thinking config files hold app logic
Confusing config files with database storage
Assuming config files replace main modules
2. Which syntax correctly creates a named custom configuration file using registerAs in NestJS?
C. export default registerAs('app', () => ({ port: 3000 }));
D. registerAs('app', () => port: 3000);
Solution
Step 1: Understand registerAs usage
The registerAs function takes a name and a function returning a config object.
Step 2: Check syntax correctness
export const config = registerAs('app', () => ({ port: 3000 })); correctly exports a constant assigned to registerAs with a function returning an object.
C. The arrow function does not return the object properly
D. The port value must be a string
Solution
Step 1: Check arrow function return
The arrow function uses curly braces without a return statement, so it returns undefined instead of the object.
Step 2: Correct syntax for returning object
To return an object directly, wrap it in parentheses: () => ({ port: 3000 }).
Final Answer:
The arrow function does not return the object properly -> Option C
Quick Check:
Arrow functions need parentheses to return objects directly [OK]
Hint: Wrap object in parentheses to return from arrow function [OK]
Common Mistakes:
Using curly braces without return in arrow functions
Assuming config name must be special
Thinking port must be string
5. You want to create two custom config files, appConfig and dbConfig, and load both in your NestJS app. Which setup correctly loads and accesses the dbConfig.username value?
hard
A. Load both configs in ConfigModule.forRoot({ load: [appConfig, dbConfig] }) and get with configService.get('dbConfig.username')
B. Load only appConfig and get dbConfig.username directly
C. Load both configs but access dbConfig.username with configService.get('username')
D. Load configs separately in different modules without forRoot
Solution
Step 1: Load multiple configs together
Use ConfigModule.forRoot({ load: [appConfig, dbConfig] }) to load both custom configs at app start.
Step 2: Access nested config by namespace
Access dbConfig.username using configService.get('dbConfig.username') because configs are namespaced by their registration name.
Final Answer:
Load both configs in ConfigModule.forRoot({ load: [appConfig, dbConfig] }) and get with configService.get('dbConfig.username') -> Option A
Quick Check:
Load all configs and use namespaced keys to access [OK]
Hint: Load all configs in forRoot and use dot keys to access [OK]
Common Mistakes:
Loading only one config but accessing another
Accessing keys without namespace prefix
Trying to load configs outside forRoot
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.