0
0
NestJSframework~20 mins

Route handlers (GET, POST, PUT, DELETE) in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Route Handler 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 GET route handler?
Consider this NestJS controller method handling a GET request. What will be the JSON response when a client calls this route?
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  getAll() {
    return { message: 'List of items', count: 3 };
  }
}
A"List of items"
B{"message":"List of items","count":3}
C{"count":3}
D[]
Attempts:
2 left
💡 Hint
Look at what the method returns and how NestJS sends JSON responses.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a POST route handler in NestJS?
You want to create a POST route handler in a NestJS controller. Which code snippet is syntactically correct?
NestJS
import { Controller, Post } from '@nestjs/common';

@Controller('users')
export class UsersController {
  // Your POST handler here
}
A
@Post()
createUser() {
  return 'User created';
}
B
@Post
createUser() {
  return 'User created';
}
C
@Post('/create')
createUser {
  return 'User created';
}
D
@Post('/create')
createUser() {
  return 'User created';
}
Attempts:
2 left
💡 Hint
Check the decorator syntax and method declaration carefully.
state_output
advanced
2:00remaining
What is the response after calling this PUT route handler?
Given this NestJS PUT route handler, what will be the JSON response when a client sends a PUT request?
NestJS
import { Controller, Put, Body } from '@nestjs/common';

@Controller('products')
export class ProductsController {
  private products = [{ id: 1, name: 'Book' }];

  @Put(':id')
  updateProduct(@Body() body: { name: string }) {
    this.products[0].name = body.name;
    return this.products[0];
  }
}
A{"id":1,"name":null}
B{"id":1,"name":"Book"}
C{"id":1,"name":"Updated"}
DSyntaxError
Attempts:
2 left
💡 Hint
The method updates the product name with the value from the request body.
🔧 Debug
advanced
2:00remaining
Which option causes an error in this DELETE route handler?
This NestJS controller has a DELETE route handler. Which option will cause a runtime error when the route is called?
NestJS
import { Controller, Delete, Param } from '@nestjs/common';

@Controller('tasks')
export class TasksController {
  private tasks = [{ id: 1, title: 'Task 1' }];

  @Delete(':id')
  deleteTask(@Param('id') id: string) {
    const index = this.tasks.findIndex(task => task.id === Number(id));
    if (index === -1) {
      throw new Error('Task not found');
    }
    this.tasks.splice(index, 1);
    return { message: 'Deleted' };
  }
}
ACalling DELETE /tasks/2 throws 'Task not found' error
BCalling DELETE /tasks/abc throws a TypeError
CCalling DELETE /tasks/0 removes the first task
DCalling DELETE /tasks/1 removes the task and returns { message: 'Deleted' }
Attempts:
2 left
💡 Hint
Consider what happens when Number(id) is NaN and how findIndex behaves.
🧠 Conceptual
expert
2:00remaining
Which HTTP method is best suited for updating part of a resource in NestJS?
In NestJS, you want to update only some fields of an existing resource without replacing it entirely. Which HTTP method and decorator should you use?
AUse @Patch() to update part of the resource
BUse @Put() to update the entire resource
CUse @Post() to update part of the resource
DUse @Delete() to update part of the resource
Attempts:
2 left
💡 Hint
Think about the difference between PUT and PATCH in HTTP methods.