Complete the code to define a NestJS controller using TypeScript decorators.
import { Controller, Get } from '@nestjs/common'; @Controller('[1]') export class AppController { @Get() getHello(): string { return 'Hello World!'; } }
The @Controller decorator takes a string that defines the route path. Here, 'app' sets the base route for this controller.
Complete the code to inject a service into a NestJS controller constructor.
import { Controller } from '@nestjs/common'; import { AppService } from './app.service'; @Controller('app') export class AppController { constructor(private readonly [1]: AppService) {} }
By convention, the injected service is named in camelCase matching the class name, here appService.
Fix the error in the following NestJS service method signature by completing the return type.
import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getData(): [1] { return { message: 'Data fetched' }; } }
string which is incorrect for an object return.any which loses type safety.The method returns an object with string keys and string values, so Record<string, string> is the precise TypeScript type.
Fill both blanks to create a NestJS DTO class with two properties having types and decorators.
import { IsString, IsInt } from 'class-validator'; export class CreateUserDto { @[1]() name: string; @[2]() age: number; }
IsNumber for age which allows floats.IsBoolean which is wrong type.IsString validates that name is a string, and IsInt validates that age is an integer number.
Fill all three blanks to complete a NestJS module importing a service and controller.
import { Module } from '@nestjs/common'; import { [1] } from './app.service'; import { [2] } from './app.controller'; @Module({ imports: [], controllers: [[3]], providers: [AppService], }) export class AppModule {}
controllers array.The module imports AppService and AppController. The controller is listed in the controllers array.