0
0
NestJSframework~20 mins

Decorator-based architecture in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS controller method?
Consider this NestJS controller method decorated with @Get and @UseGuards. What will be the HTTP response status code when this endpoint is accessed successfully?
NestJS
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Controller('items')
export class ItemsController {
  @Get()
  @UseGuards(AuthGuard('jwt'))
  findAll() {
    return { message: 'Items fetched' };
  }
}
A200 OK with JSON { message: 'Items fetched' }
B401 Unauthorized error
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about what @UseGuards(AuthGuard('jwt')) does when the user is authenticated.
📝 Syntax
intermediate
2:00remaining
Which option correctly applies a custom decorator to inject a user object in NestJS?
You want to create a custom decorator @CurrentUser() that extracts the user from the request object. Which option correctly defines and uses this decorator in a controller method?
NestJS
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const CurrentUser = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
  const request = ctx.switchToHttp().getRequest();
  return request.user;
});

@Controller('profile')
export class ProfileController {
  @Get()
  getProfile(@CurrentUser() user) {
    return user;
  }
}
ACorrect as shown in the code above
BReplace createParamDecorator with @Injectable decorator
CUse @Param('user') instead of @CurrentUser()
DDefine CurrentUser as a class instead of a function
Attempts:
2 left
💡 Hint
Custom parameter decorators use createParamDecorator function.
🔧 Debug
advanced
2:00remaining
Why does this NestJS service fail to inject the repository?
Given this service code, why will NestJS throw a runtime error about missing dependencies?
NestJS
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(@InjectRepository(User) private userRepository: Repository<User>) {}

  findAll() {
    return this.userRepository.find();
  }
}
AThe findAll method is missing async keyword
BThe @Injectable decorator is missing
CThe User entity is not imported correctly
DThe constructor parameter is missing @InjectRepository(User) decorator
Attempts:
2 left
💡 Hint
Check how TypeORM repositories are injected in NestJS services.
lifecycle
advanced
2:00remaining
What happens when a NestJS provider implements OnModuleInit?
If a service class implements the OnModuleInit interface and defines the onModuleInit() method, when is this method called?
NestJS
import { Injectable, OnModuleInit } from '@nestjs/common';

@Injectable()
export class AppService implements OnModuleInit {
  onModuleInit() {
    console.log('Module initialized');
  }
}
ACalled every time a request is received
BCalled once after the module's providers are initialized
CCalled only when the application shuts down
DCalled before the module's providers are created
Attempts:
2 left
💡 Hint
Think about the lifecycle of modules and providers in NestJS.
🧠 Conceptual
expert
2:00remaining
Which option best describes the role of decorators in NestJS architecture?
In NestJS, decorators like @Controller, @Injectable, and @Module are used extensively. What is their primary purpose in the framework's architecture?
AThey execute code immediately to modify class behavior at runtime
BThey replace the need for writing any configuration files
CThey add metadata to classes and methods to enable dependency injection and routing
DThey automatically generate database schemas from classes
Attempts:
2 left
💡 Hint
Think about how NestJS uses decorators to organize and connect parts of the app.