Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a dynamic module with a static method.
NestJS
import { Module, DynamicModule } from '@nestjs/common'; @Module({}) export class ConfigModule { static forRoot(): [1] { return { module: ConfigModule, }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a plain Module instead of DynamicModule
Forgetting to return an object from the static method
✗ Incorrect
The static method forRoot() must return a DynamicModule to configure the module dynamically.
2fill in blank
mediumComplete the code to inject a configuration value into the dynamic module.
NestJS
import { Module, DynamicModule } from '@nestjs/common'; @Module({}) export class ConfigModule { static forRoot(configValue: string): DynamicModule { return { module: ConfigModule, providers: [ { provide: 'CONFIG_VALUE', useValue: [1], }, ], exports: ['CONFIG_VALUE'], }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable
Using the module name instead of the config value
✗ Incorrect
The useValue should be the parameter configValue passed to the static method.
3fill in blank
hardFix the error in the dynamic module import syntax.
NestJS
import { Module, DynamicModule } from '@nestjs/common'; import { ConfigModule } from './config.module'; @Module({ imports: [ConfigModule.[1]({ apiKey: '123' })], }) export class AppModule {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using register or create which are not standard method names here
Using useFactory which is for providers, not module imports
✗ Incorrect
The dynamic module method to import is usually named forRoot to configure the module.
4fill in blank
hardFill both blanks to create a dynamic module that imports another module and provides a service.
NestJS
import { Module, DynamicModule } from '@nestjs/common'; import { HttpModule } from '@nestjs/axios'; @Module({}) export class ApiModule { static forRoot(apiUrl: string): DynamicModule { return { module: ApiModule, imports: [[1]], providers: [ { provide: 'API_URL', useValue: [2], }, ], exports: ['API_URL'], }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for useValue
Importing the wrong module
✗ Incorrect
The imports array should include HttpModule, and the useValue should be the apiUrl parameter.
5fill in blank
hardFill all three blanks to create a dynamic module that registers a provider with a factory and injects a token.
NestJS
import { Module, DynamicModule } from '@nestjs/common'; @Module({}) export class LoggerModule { static forRoot(level: string): DynamicModule { return { module: LoggerModule, providers: [ { provide: 'LOG_LEVEL', useValue: [1], }, { provide: 'LOGGER_SERVICE', useFactory: (logLevel: string) => new LoggerService(logLevel), inject: [[2]], }, ], exports: [[3]], }; } } class LoggerService { constructor(private level: string) {} }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting the service token instead of the log level token
Exporting the log level token instead of the service token
✗ Incorrect
useValue should be the level parameter; inject array should contain 'LOG_LEVEL'; exports should include 'LOGGER_SERVICE'.