0
0
NestJSframework~10 mins

Why configuration management matters in NestJS - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the ConfigModule in a NestJS application.

NestJS
import { Module } from '@nestjs/common';
import { [1] } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
ADatabaseModule
BHttpModule
CAuthModule
DConfigModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like HttpModule instead of ConfigModule.
Forgetting to import ConfigModule causes configuration to not load.
2fill in blank
medium

Complete the code to access a configuration value using ConfigService.

NestJS
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

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

  getDatabaseHost(): string {
    return this.configService.[1]('DATABASE_HOST');
  }
}
Drag options to blanks, or click blank then click option'
Aget
Bset
Cload
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'get' which causes errors.
Trying to access config values directly without ConfigService.
3fill in blank
hard

Fix the error in the code to load environment variables from a custom path.

NestJS
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: [1],
    }),
  ],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
Aenv.production
B'.env.production'
C'env.production'
D.env.production
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting quotes causes syntax errors.
Using variable names without quotes instead of string paths.
4fill in blank
hard

Fill both blanks to create a configuration object with a default value and type safety.

NestJS
import { registerAs } from '@nestjs/config';

export default registerAs('database', () => ({
  host: process.env.DB_HOST || [1],
  port: Number(process.env.DB_PORT) || [2],
}));
Drag options to blanks, or click blank then click option'
A'localhost'
B5432
C3306
D'127.0.0.1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using port as a string instead of a number.
Forgetting to provide default values causing undefined errors.
5fill in blank
hard

Fill all three blanks to create a typed configuration interface and use it in a service.

NestJS
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

interface AppConfig {
  apiKey: string;
  timeout: number;
}

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

  getApiKey(): string {
    return this.configService.[1]<[2]>('API_KEY');
  }

  getTimeout(): number {
    return this.configService.get<[3]>('TIMEOUT');
  }
}
Drag options to blanks, or click blank then click option'
Aget
BAppConfig
Cnumber
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fetch' instead of 'get' causes errors.
Not specifying generic types loses type safety.