📖 Scenario: You are building a simple API for managing a list of books in a library. Each book has a title and an author.This API will let users get the list of books, add a new book, update an existing book, and delete a book.
🎯 Goal: Create a NestJS controller with route handlers for GET, POST, PUT, and DELETE to manage books.
📋 What You'll Learn
Create a controller class named BooksController.
Create a property books initialized with an array of two book objects: { id: 1, title: '1984', author: 'George Orwell' } and { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien' }.
Add a GET route handler method getAllBooks() that returns the books array.
Add a POST route handler method addBook() that accepts a book object and adds it to books.
Add a PUT route handler method updateBook() that accepts an id parameter and a book object, then updates the matching book in books.
Add a DELETE route handler method deleteBook() that accepts an id parameter and removes the matching book from books.
💡 Why This Matters
🌍 Real World
APIs like this are common in web applications to manage data resources such as books, users, or products.
💼 Career
Understanding how to create route handlers for different HTTP methods is essential for backend development roles using NestJS or similar frameworks.
Progress0 / 4 steps
1
Create the initial books data array
Create a controller class named BooksController with a property books initialized to an array containing exactly these two objects: { id: 1, title: '1984', author: 'George Orwell' } and { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien' }.
NestJS
Hint
Define a class named BooksController. Inside it, create a property books and assign the array with the two book objects.
2
Add a GET route handler to return all books
Add a method named getAllBooks() decorated with @Get() that returns the books array.
NestJS
Hint
Use the @Get() decorator above the getAllBooks() method. Return the books property inside the method.
3
Add POST and PUT route handlers to add and update books
Add a POST route handler method addBook() decorated with @Post() that accepts a book object parameter decorated with @Body() and adds it to books. Also add a PUT route handler method updateBook() decorated with @Put(':id') that accepts an id parameter decorated with @Param('id') and a book object decorated with @Body(), then updates the matching book in books.
NestJS
Hint
Use @Post() and @Put(':id') decorators. Use @Body() to get the book data and @Param('id') to get the id. Update the books array accordingly.
4
Add DELETE route handler to remove a book
Add a DELETE route handler method deleteBook() decorated with @Delete(':id') that accepts an id parameter decorated with @Param('id') and removes the matching book from books.
NestJS
Hint
Use the @Delete(':id') decorator. Use @Param('id') to get the id. Use filter to remove the book from the array.
Practice
(1/5)
1. Which decorator is used in NestJS to create a route handler that responds to HTTP GET requests?
easy
A. @Get
B. @Post
C. @Put
D. @Delete
Solution
Step 1: Understand HTTP methods and their decorators
In NestJS, each HTTP method has a specific decorator: @Get for GET, @Post for POST, @Put for PUT, and @Delete for DELETE.
Step 2: Match the decorator to the GET method
The decorator @Get is specifically designed to handle HTTP GET requests.
Final Answer:
@Get -> Option A
Quick Check:
GET method uses @Get decorator [OK]
Hint: GET requests use @Get decorator in NestJS [OK]
Common Mistakes:
Confusing @Post or @Put with @Get
Using @Delete for GET requests
Not using any decorator
2. Which of the following is the correct syntax to define a POST route handler in a NestJS controller?
easy
A. @Post('/create') createItem() {}
B. Post('/create') createItem() {}
C. @post('/create') createItem() {}
D. @Put('/create') createItem() {}
Solution
Step 1: Check the decorator syntax
Decorators in NestJS start with '@' and are case-sensitive. The correct decorator for POST is '@Post'.
Step 2: Verify the route path and method name
The route path '/create' is correctly passed as a string argument, and the method name is valid.
Final Answer:
@Post('/create') createItem() {} -> Option A
Quick Check:
Correct decorator syntax uses '@Post' with parentheses [OK]
Hint: Decorators start with '@' and are case-sensitive [OK]
Common Mistakes:
Missing '@' before decorator
Using lowercase '@post' instead of '@Post'
Using wrong decorator like '@Put' for POST
3. Given the following NestJS controller code, what will be the HTTP method and route path to access the handler?
A. The method name must be 'deleteItem' for @Delete decorator
B. The decorator @Delete cannot have a path argument
C. The controller path 'items' is invalid
D. The method should return a value, not just console.log
Solution
Step 1: Check the decorator and path usage
@Delete can have a path argument like '/remove', so this is valid.
Step 2: Check the method implementation
The method only logs to console but does not return any response. HTTP handlers should return a response to the client.
Final Answer:
The method should return a value, not just console.log -> Option D
Quick Check:
Route handlers must return a response [OK]
Hint: Route handlers must return a response, not just log [OK]
Common Mistakes:
Assuming console.log sends response
Thinking decorator cannot have path
Believing method name must match decorator
5. You want to create a NestJS controller with a route handler that updates a product by its ID using HTTP PUT. The handler should accept the ID as a route parameter and the updated data in the request body. Which code snippet correctly implements this?