0
0
NestJSframework~30 mins

Feature modules in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Feature Module in NestJS
📖 Scenario: You are creating a simple NestJS application to manage books in a library. To keep the code organized, you want to create a feature module dedicated to books.
🎯 Goal: Build a NestJS feature module named BooksModule that includes a service and a controller for managing books.
📋 What You'll Learn
Create a BooksModule class decorated with @Module
Add a BooksService class with a method getBooks() returning a list of book titles
Add a BooksController class with a GET route /books that returns the list of books from BooksService
Import the BooksModule into the root AppModule
💡 Why This Matters
🌍 Real World
Feature modules help organize code in large NestJS applications by grouping related controllers and services.
💼 Career
Understanding feature modules is essential for building scalable and maintainable backend applications with NestJS, a popular Node.js framework.
Progress0 / 4 steps
1
Create the BooksService with a getBooks method
Create a class called BooksService with a method getBooks() that returns an array of strings: ["The Hobbit", "1984", "Clean Code"].
NestJS
Need a hint?

Use @Injectable() decorator on the service class.

2
Create the BooksController with a GET /books route
Create a class called BooksController decorated with @Controller('books'). Inject BooksService in the constructor. Add a method getAllBooks() decorated with @Get() that returns the result of this.booksService.getBooks().
NestJS
Need a hint?

Use dependency injection in the controller constructor.

3
Create the BooksModule and add service and controller
Create a class called BooksModule decorated with @Module. Add BooksService to the providers array and BooksController to the controllers array.
NestJS
Need a hint?

Use @Module decorator with providers and controllers arrays.

4
Import BooksModule into AppModule
Modify the existing AppModule class decorated with @Module to import BooksModule in the imports array.
NestJS
Need a hint?

Remember to import BooksModule and add it to the imports array.