0
0
NestJSframework~10 mins

TypeScript-first philosophy 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 TypeScript decorators.

NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('[1]')
export class AppController {
  @Get()
  getHello(): string {
    return 'Hello World!';
  }
}
Drag options to blanks, or click blank then click option'
Aapp
BAppController
Chello
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of a route string.
Leaving the decorator empty which defaults to root.
2fill in blank
medium

Complete the code to inject a service into a NestJS controller constructor.

NestJS
import { Controller } from '@nestjs/common';
import { AppService } from './app.service';

@Controller('app')
export class AppController {
  constructor(private readonly [1]: AppService) {}
}
Drag options to blanks, or click blank then click option'
AappService
BAppService
Cappservice
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with uppercase first letter as variable name.
Using a generic name like 'service' which is unclear.
3fill in blank
hard

Fix the error in the following NestJS service method signature by completing the return type.

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

@Injectable()
export class AppService {
  getData(): [1] {
    return { message: 'Data fetched' };
  }
}
Drag options to blanks, or click blank then click option'
Aany
Bobject
Cstring
DRecord<string, string>
Attempts:
3 left
💡 Hint
Common Mistakes
Using string which is incorrect for an object return.
Using any which loses type safety.
4fill in blank
hard

Fill both blanks to create a NestJS DTO class with two properties having types and decorators.

NestJS
import { IsString, IsInt } from 'class-validator';

export class CreateUserDto {
  @[1]()
  name: string;

  @[2]()
  age: number;
}
Drag options to blanks, or click blank then click option'
AIsString
BIsNumber
CIsInt
DIsBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using IsNumber for age which allows floats.
Using IsBoolean which is wrong type.
5fill in blank
hard

Fill all three blanks to complete a NestJS module importing a service and controller.

NestJS
import { Module } from '@nestjs/common';
import { [1] } from './app.service';
import { [2] } from './app.controller';

@Module({
  imports: [],
  controllers: [[3]],
  providers: [AppService],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AAppService
BAppController
DAppModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using the module class name in imports or controllers.
Forgetting to add the controller to the controllers array.