0
0
NestJSframework~30 mins

Route parameters in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Handling Route Parameters in NestJS
📖 Scenario: You are building a simple NestJS API for a bookstore. You want to create a route that can receive a book's ID as a parameter in the URL and return details about that book.
🎯 Goal: Build a NestJS controller with a route that accepts a bookId as a route parameter and returns a message including that bookId.
📋 What You'll Learn
Create a controller named BooksController.
Add a route handler method named getBookById.
Use the @Param decorator to extract the bookId from the route.
Return a string message that includes the bookId.
💡 Why This Matters
🌍 Real World
Route parameters are used in web APIs to identify specific resources, like books, users, or orders, by their unique IDs in the URL.
💼 Career
Understanding route parameters is essential for backend developers building RESTful APIs with frameworks like NestJS.
Progress0 / 4 steps
1
Create the BooksController class
Create a class called BooksController and decorate it with @Controller('books').
NestJS
Need a hint?

Use @Controller('books') above the class to define the route prefix.

2
Add the getBookById method with route parameter
Inside BooksController, add a method called getBookById decorated with @Get(':bookId') to handle GET requests with a bookId parameter.
NestJS
Need a hint?

Use @Get(':bookId') to define the route with a parameter named bookId.

3
Extract the bookId parameter using @Param decorator
Modify the getBookById method to accept a parameter called bookId using the @Param('bookId') decorator from @nestjs/common.
NestJS
Need a hint?

Import @Param and use it to get the bookId from the URL.

4
Return a message including the bookId
Inside the getBookById method, return a string like `Book ID requested is: ${bookId}` that includes the bookId parameter.
NestJS
Need a hint?

Use a template string to include the bookId in the returned message.