Basic CRUD API with NestJS Route Handlers
📖 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