0
0
NestJSframework~10 mins

Decorator-based architecture 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 define a NestJS controller using the correct decorator.

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

@[1]('cats')
export class CatsController {
  // controller methods
}
Drag options to blanks, or click blank then click option'
AService
BInjectable
CModule
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Injectable instead of @Controller
Confusing @Module with @Controller
2fill in blank
medium

Complete the code to create a GET route handler in a NestJS controller.

NestJS
import { Controller, [1] } from '@nestjs/common';

@Controller('dogs')
export class DogsController {
  @[1]()
  findAll() {
    return 'This action returns all dogs';
  }
}
Drag options to blanks, or click blank then click option'
ADelete
BPost
CGet
DPut
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Post instead of @Get for a GET route
Forgetting to import the decorator
3fill in blank
hard

Fix the error in the service class by adding the correct decorator.

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

export class CatsService {
  getCats() {
    return ['cat1', 'cat2'];
  }
}
Drag options to blanks, or click blank then click option'
AInjectable
BController
CModule
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller on a service class
Not adding any decorator to the service
4fill in blank
hard

Fill both blanks to define a NestJS module with a controller and a provider.

NestJS
import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

@Module({
  controllers: [[1]],
  providers: [[2]],
})
export class CatsModule {}
Drag options to blanks, or click blank then click option'
ACatsController
BCatsService
CDogsController
DDogsService
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing controllers and providers
Using unrelated classes in the arrays
5fill in blank
hard

Fill all three blanks to create a route handler that accepts a dynamic parameter and injects a service.

NestJS
import { Controller, Get, Param, [1] } from '@nestjs/common';
import { [3] } from '@nestjs/common';

@[3]()
export class CatsService {
  [2](id) {
    return 'Cat ' + id;
  }
}

@Controller('cats')
export class CatsController {
  constructor(@[1]() private readonly catsService: CatsService) {}

  @Get(':id')
  getCat(@Param('id') id: string) {
    return this.catsService.[2](id);
  }
}
Drag options to blanks, or click blank then click option'
AInjectable
BfindOne
CInject
DgetCats
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Injectable in the controller instead of the service
Calling a non-existent service method
Not importing the correct decorators