Bird
Raised Fist0
NestJSframework~20 mins

First NestJS application - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS Novice 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 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!';
  }
}
A"Hello, NestJS!"
B"{ message: 'Hello, NestJS!' }"
CAn empty response with status 200
DA 404 Not Found error
Attempts:
2 left
💡 Hint
Look at the return value of the getHello() method and how NestJS sends it as the response body.
❓ state_output
intermediate
2: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();
  }
}
AAn error occurs because 'count' is private
B3
C1
D0
Attempts:
2 left
💡 Hint
The service instance is shared and 'count' is incremented each time increment() is called.
📝 Syntax
advanced
2: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.
A
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
B
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [AppController],
  providers: [AppService],
})
export class AppModule {}
C
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppService],
  providers: [AppController],
})
export class AppModule {}
D
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: AppController,
  providers: AppService,
})
export class AppModule {}
Attempts:
2 left
💡 Hint
Controllers go in 'controllers' array, services in 'providers' array, both as arrays.
🔧 Debug
advanced
2: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 {}
AAppController is missing from controllers array
BThe import statements are incorrect
CThe @Module decorator is missing parentheses
DAppService is not listed in providers, so dependency injection fails if AppController depends on it
Attempts:
2 left
💡 Hint
Check if all dependencies used by controllers are provided in the module.
🧠 Conceptual
expert
3: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;
  }
}
AThe service is not injectable and causes a compile-time error
BThe service behaves as a singleton shared across all requests
CA new instance of the service is created for each incoming HTTP request
DThe service instance is created once per application startup
Attempts:
2 left
💡 Hint
Think about how scope affects instance creation in NestJS.

Practice

(1/5)
1. What is the main purpose of a controller in a NestJS application?
easy
A. To configure the server settings and environment variables
B. To connect to the database and manage data storage
C. To handle incoming HTTP requests and send responses
D. To define the application's routing paths only

Solution

  1. Step 1: Understand the role of controllers

    Controllers in NestJS are designed to receive HTTP requests and return responses to the client.
  2. Step 2: Differentiate from other components

    Modules organize the app, services handle business logic, and controllers handle requests and responses.
  3. Final Answer:

    To handle incoming HTTP requests and send responses -> Option C
  4. Quick Check:

    Controller = Request handler [OK]
Hint: Controllers respond to HTTP requests directly [OK]
Common Mistakes:
  • Confusing controllers with modules
  • Thinking controllers manage database connections
  • Assuming controllers only define routes without logic
2. Which of the following is the correct way to start a NestJS application in the main.ts file?
easy
A. app.start(3000);
B. await app.listen(3000);
C. app.run(3000);
D. app.open(3000);

Solution

  1. Step 1: Identify the bootstrap function usage

    The NestJS app instance uses the listen() method to start the server on a specified port.
  2. Step 2: Confirm the correct syntax

    The correct syntax is await app.listen(3000); to asynchronously start the server on port 3000.
  3. Final Answer:

    await app.listen(3000); -> Option B
  4. Quick Check:

    Start server with listen() [OK]
Hint: Use listen() method to start the server [OK]
Common Mistakes:
  • Using non-existent methods like start() or run()
  • Forgetting to await the listen() call
  • Confusing listen() with open()
3. Given this controller code snippet, what will be the HTTP response when a GET request is made to '/'?
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  getHello(): string {
    return 'Hello, NestJS!';
  }
}
medium
A. No response because method is not async
B. An error because @Get decorator is missing a path
C. "GET request received"
D. "Hello, NestJS!"

Solution

  1. Step 1: Analyze the controller method

    The method getHello() is decorated with @Get() without a path, so it responds to GET requests at the root path '/'.
  2. Step 2: Understand the returned value

    The method returns the string 'Hello, NestJS!', which will be sent as the HTTP response body.
  3. Final Answer:

    "Hello, NestJS!" -> Option D
  4. Quick Check:

    @Get() returns method string response [OK]
Hint: @Get() without path means root '/' route [OK]
Common Mistakes:
  • Thinking @Get() requires a path argument
  • Assuming async is required for response
  • Expecting JSON response instead of string
4. What is wrong with this main.ts file that prevents the NestJS app from starting?
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.listen(3000);
}

bootstrap();
medium
A. Missing await before app.listen(3000);
B. AppModule is not imported correctly
C. bootstrap function is not called
D. NestFactory.create should be synchronous

Solution

  1. Step 1: Check async function usage

    The bootstrap function is async, so asynchronous calls inside should be awaited to ensure proper execution.
  2. Step 2: Identify missing await

    The call app.listen(3000); returns a Promise and should be awaited as await app.listen(3000); to start the server correctly.
  3. Final Answer:

    Missing await before app.listen(3000); -> Option A
  4. Quick Check:

    Always await async listen() call [OK]
Hint: Always await app.listen() in async bootstrap [OK]
Common Mistakes:
  • Not awaiting app.listen() causing server not to start
  • Assuming NestFactory.create is synchronous
  • Forgetting to call bootstrap()
5. You want to create a simple NestJS app that responds with "Welcome to NestJS!" when a user visits '/welcome'. Which code snippet correctly implements this in the controller?
hard
A. import { Controller, Get } from '@nestjs/common'; @Controller() export class WelcomeController { @Get('welcome') welcome() { return 'Welcome to NestJS!'; } }
B. import { Controller, Get } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Get('/') welcome() { return 'Welcome to NestJS!'; } }
C. import { Controller, Post } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Post() welcome() { return 'Welcome to NestJS!'; } }
D. import { Controller, Get } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Get('welcome') welcome() { return 'Welcome to NestJS!'; } }

Solution

  1. Step 1: Understand route path composition

    The full route path is formed by combining @Controller() path and @Get() path.
  2. Step 2: Match the desired route '/welcome'

    Using @Controller() with no path and @Get('welcome') creates the route '/welcome'. import { Controller, Get } from '@nestjs/common'; @Controller() export class WelcomeController { @Get('welcome') welcome() { return 'Welcome to NestJS!'; } } does this correctly.
  3. Step 3: Check other options

    import { Controller, Get } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Get('/') welcome() { return 'Welcome to NestJS!'; } } creates '/welcome' + '/' = '/welcome/'. import { Controller, Post } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Post() welcome() { return 'Welcome to NestJS!'; } } uses POST instead of GET. import { Controller, Get } from '@nestjs/common'; @Controller('welcome') export class WelcomeController { @Get('welcome') welcome() { return 'Welcome to NestJS!'; } } creates '/welcome/welcome'.
  4. Final Answer:

    @Controller() with @Get('welcome') -> Option A
  5. Quick Check:

    Use @Get('welcome') with empty @Controller() for '/welcome' [OK]
Hint: Combine @Controller() and @Get() paths carefully [OK]
Common Mistakes:
  • Using @Post instead of @Get for GET requests
  • Duplicating path in both @Controller and @Get
  • Assuming @Controller('welcome') alone sets full path
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard