0
0
NestJSframework~10 mins

Custom configuration files in NestJS - Interactive Code Practice

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

Complete the code to import the ConfigModule with a custom configuration file.

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

@Module({
  imports: [ConfigModule.forRoot({
    load: [[1]],
  })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AConfigModule
BConfigService
CconfigFile
DcustomConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Passing ConfigService instead of the config function.
Using ConfigModule inside the load array.
Passing a string instead of a function or object.
2fill in blank
medium

Complete the code to create a custom configuration file exporting a function.

NestJS
export default function [1]() {
  return {
    port: parseInt(process.env.PORT, 10) || 3000,
  };
}
Drag options to blanks, or click blank then click option'
Aconfig
BConfigModule
CcustomConfig
DConfigService
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConfigModule as function name.
Not exporting a default function.
Returning a non-object value.
3fill in blank
hard

Fix the error in the code to inject the ConfigService and get the custom port value.

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

@Injectable()
export class AppService {
  constructor(private readonly configService: [1]) {}

  getPort(): number {
    return this.configService.get<number>('port');
  }
}
Drag options to blanks, or click blank then click option'
AConfigModule
BConfigService
CcustomConfig
DConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting ConfigModule instead of ConfigService.
Using the config function name as type.
Forgetting to mark the constructor parameter as readonly.
4fill in blank
hard

Fill both blanks to create a custom configuration file that reads a database URL and exports it.

NestJS
export default function [1]() {
  return {
    databaseUrl: process.env.[2] || 'mongodb://localhost:27017',
  };
}
Drag options to blanks, or click blank then click option'
AdatabaseConfig
BDATABASE_URL
CDB_URL
Dconfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or camelCase for environment variable names.
Using generic function names like config.
Forgetting to export a default function.
5fill in blank
hard

Fill all three blanks to use the custom configuration in a service and get the database URL.

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

@Injectable()
export class DatabaseService {
  constructor(private readonly [2]: [1]) {}

  getDatabaseUrl(): string {
    return this.[2].get<string>('databaseUrl');
  }
}
Drag options to blanks, or click blank then click option'
AConfigService
BconfigService
CdatabaseConfig
DConfigModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConfigModule instead of ConfigService.
Naming the injected variable the same as the class.
Importing the custom config function instead of ConfigService.