Challenge - 5 Problems
NestJS Novice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this simple NestJS controller?
Consider this NestJS controller code. What will be the HTTP response body when a GET request is made to the root path '/'?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller() export class AppController { @Get() getHello(): string { return 'Hello, NestJS!'; } }
Attempts:
2 left
💡 Hint
Look at the return value of the getHello() method and how NestJS sends it as the response body.
✗ Incorrect
The getHello() method returns a string. NestJS sends this string as the HTTP response body directly, so the client receives "Hello, NestJS!".
❓ state_output
intermediate2:00remaining
What is the value of 'count' after three POST requests?
Given this NestJS service and controller, what will be the value of 'count' after three POST requests to '/increment'?
NestJS
import { Injectable, Controller, Post } from '@nestjs/common'; @Injectable() export class CounterService { private count = 0; increment() { this.count++; return this.count; } getCount() { return this.count; } } @Controller() export class CounterController { constructor(private readonly counterService: CounterService) {} @Post('increment') increment() { return this.counterService.increment(); } }
Attempts:
2 left
💡 Hint
The service instance is shared and 'count' is incremented each time increment() is called.
✗ Incorrect
The CounterService is a singleton by default in NestJS. Each POST request calls increment(), increasing 'count' by 1. After three calls, 'count' is 3.
📝 Syntax
advanced2:00remaining
Which option correctly defines a NestJS module with a controller and service?
Identify the correct NestJS module definition that properly imports the controller and service.
Attempts:
2 left
💡 Hint
Controllers go in 'controllers' array, services in 'providers' array, both as arrays.
✗ Incorrect
The @Module decorator expects arrays for 'controllers' and 'providers'. Option A correctly assigns AppController to controllers and AppService to providers as arrays.
🔧 Debug
advanced2:00remaining
Why does this NestJS app throw a runtime error on startup?
Given this code snippet, why does the NestJS application fail to start?
NestJS
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ controllers: [AppController], providers: [], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Check if all dependencies used by controllers are provided in the module.
✗ Incorrect
If AppController injects AppService but AppService is not in providers, NestJS cannot resolve the dependency, causing a runtime error.
🧠 Conceptual
expert3:00remaining
What happens if you mark a NestJS service as @Injectable({ scope: Scope.REQUEST })?
In NestJS, what is the effect of setting a service's scope to REQUEST using @Injectable({ scope: Scope.REQUEST })?
NestJS
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.REQUEST }) export class RequestScopedService { private readonly id = Math.random(); getId() { return this.id; } }
Attempts:
2 left
💡 Hint
Think about how scope affects instance creation in NestJS.
✗ Incorrect
Setting scope to REQUEST means NestJS creates a fresh instance of the service for every HTTP request, isolating state per request.