0
0
NestJSframework~10 mins

Dynamic modules 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 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'
ADynamicModule
BModule
CProvider
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a plain Module instead of DynamicModule
Forgetting to return an object from the static method
2fill in blank
medium

Complete 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'
A'configValue'
BConfigModule
CconfigValue
DCONFIG_VALUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable
Using the module name instead of the config value
3fill in blank
hard

Fix 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'
Aregister
BforRoot
CuseFactory
Dcreate
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
4fill in blank
hard

Fill 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'
AHttpModule
BapiUrl
CConfigModule
D'apiUrl'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for useValue
Importing the wrong module
5fill in blank
hard

Fill 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'
Alevel
B'LOG_LEVEL'
C'LOGGER_SERVICE'
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