📖 Scenario: You are building a NestJS application that needs to load custom configuration settings from a separate file. This helps keep your app settings organized and easy to change without touching the main code.
🎯 Goal: Create a custom configuration file and load it into your NestJS app using the ConfigModule. You will define a config file, import it, and access its values in a service.
📋 What You'll Learn
Create a configuration file named app.config.ts exporting a function returning an object with a port number and database object with host and port.
Import the custom config file into the ConfigModule.forRoot() using the load option.
Inject ConfigService into a service and read the port and database.host values.
Log the loaded configuration values inside the service constructor.
💡 Why This Matters
🌍 Real World
Custom configuration files help keep app settings organized and separate from code. This makes apps easier to maintain and configure for different environments.
💼 Career
Many backend jobs require managing app settings cleanly. Knowing how to use NestJS ConfigModule with custom config files is a valuable skill for building scalable, maintainable applications.
Progress0 / 4 steps
1
Create the custom configuration file
Create a file named app.config.ts that exports a default function returning an object with these exact properties: port set to 3000, and database as an object with host set to 'localhost' and port set to 5432.
NestJS
Hint
Use export default () => ({ ... }) to export the config object.
2
Load the custom config in ConfigModule
In your main module file (e.g., app.module.ts), import ConfigModule from @nestjs/config. Then, call ConfigModule.forRoot() with the load option set to an array containing the imported app.config function.
NestJS
Hint
Use ConfigModule.forRoot({ load: [appConfig] }) inside the imports array.
3
Inject ConfigService and read config values
Create a service named AppService. Inject ConfigService from @nestjs/config in its constructor. Inside the constructor, read the port value using this.configService.get('port') and the database.host value using this.configService.get('database.host'). Store these values in class properties named appPort and dbHost respectively.
NestJS
Hint
Use this.configService.get('port') and this.configService.get('database.host') inside the constructor.
4
Log the configuration values in the service
Inside the AppService constructor, add two console.log statements to print the appPort and dbHost values with these exact messages: App running on port: {appPort} and Database host: {dbHost}.
NestJS
Hint
Use template strings inside console.log to show the values.
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.