Custom configuration files help you organize settings for your NestJS app. They keep your code clean and make it easy to change settings without touching the main code.
Custom configuration files in NestJS
import { registerAs } from '@nestjs/config'; export default registerAs('customConfig', () => ({ key: 'value', anotherKey: 123 }));
Use registerAs to create a named configuration file.
The function returns an object with your settings.
import { registerAs } from '@nestjs/config'; export default registerAs('database', () => ({ host: 'localhost', port: 5432, username: 'user', password: 'pass' }));
import { registerAs } from '@nestjs/config'; export default registerAs('api', () => ({ key: process.env.API_KEY || 'defaultKey', timeout: 5000 }));
This module loads the custom config file and prints the value of key from it.
import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import customConfig from './custom.config'; @Module({ imports: [ ConfigModule.forRoot({ load: [customConfig] }) ] }) export class AppModule { constructor(private configService: ConfigService) { const value = this.configService.get('customConfig.key'); console.log('Custom config key:', value); } }
Always keep sensitive data like passwords in environment variables, not in config files.
You can create multiple custom config files and load them all in ConfigModule.forRoot.
Access config values using dot notation with ConfigService.get().
Custom config files keep your app settings organized and separate from code.
Use registerAs to create named config files that return settings objects.
Load custom configs with ConfigModule.forRoot({ load: [...] }) and access them via ConfigService.
Practice
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 BQuick Check:
Custom config files organize settings [OK]
- Thinking config files hold app logic
- Confusing config files with database storage
- Assuming config files replace main modules
registerAs in NestJS?Solution
Step 1: Understand registerAs usage
TheregisterAsfunction 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 toregisterAswith a function returning an object.Final Answer:
export const config = registerAs('app', () => ({ port: 3000 })); -> Option AQuick Check:
registerAs needs a name and a function returning an object [OK]
- Omitting export or const keywords
- Passing object directly instead of a function
- Incorrect arrow function syntax
export const databaseConfig = registerAs('database', () => ({ host: 'localhost', port: 5432 }));And this module setup:
ConfigModule.forRoot({ load: [databaseConfig] })What will
configService.get('database.host') return?Solution
Step 1: Understand config loading
ThedatabaseConfigis loaded into ConfigModule withload, making its keys accessible.Step 2: Access nested config value
UsingconfigService.get('database.host')accesses the 'host' key inside the 'database' config, which is 'localhost'.Final Answer:
'localhost' -> Option DQuick Check:
Nested config keys return their values [OK]
- Expecting the whole object instead of a value
- Using wrong key names
- Not loading config before accessing
export const appConfig = registerAs('app', () => { port: 3000 });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 CQuick Check:
Arrow functions need parentheses to return objects directly [OK]
- Using curly braces without return in arrow functions
- Assuming config name must be special
- Thinking port must be string
appConfig and dbConfig, and load both in your NestJS app. Which setup correctly loads and accesses the dbConfig.username value?Solution
Step 1: Load multiple configs together
UseConfigModule.forRoot({ load: [appConfig, dbConfig] })to load both custom configs at app start.Step 2: Access nested config by namespace
AccessdbConfig.usernameusingconfigService.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 AQuick Check:
Load all configs and use namespaced keys to access [OK]
- Loading only one config but accessing another
- Accessing keys without namespace prefix
- Trying to load configs outside forRoot
Ready to prove it?
Pass the test and this pattern moves unknown
