0
0
NestJSframework~30 mins

Decorator-based architecture in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Simple Service with Decorator-based Architecture in NestJS
📖 Scenario: You are creating a small NestJS service that manages a list of books. You will use decorators to define a service class and a method that returns the list of books.
🎯 Goal: Build a NestJS service class called BooksService using the @Injectable() decorator. Inside it, create a method getBooks() that returns an array of book titles.
📋 What You'll Learn
Create a class named BooksService.
Use the @Injectable() decorator on the BooksService class.
Inside BooksService, create a method called getBooks().
The getBooks() method should return an array of strings: ["The Hobbit", "1984", "Clean Code"].
💡 Why This Matters
🌍 Real World
Services in NestJS are used to organize business logic and data access. Using decorators like @Injectable() helps NestJS manage dependencies automatically.
💼 Career
Understanding decorator-based architecture in NestJS is essential for backend development roles using this popular framework. It helps build clean, maintainable, and testable server-side applications.
Progress0 / 4 steps
1
Create the BooksService class
Create a class called BooksService.
NestJS
Need a hint?

Use the class keyword followed by BooksService and curly braces.

2
Add the @Injectable() decorator
Add the @Injectable() decorator above the BooksService class. Also, import Injectable from @nestjs/common.
NestJS
Need a hint?

Place @Injectable() right before the class declaration.

3
Create the getBooks() method
Inside the BooksService class, create a method called getBooks() that returns the array ["The Hobbit", "1984", "Clean Code"].
NestJS
Need a hint?

Define a method named getBooks() inside the class that returns the exact array.

4
Export the BooksService class
Ensure the BooksService class is exported using the export keyword so it can be used in other parts of the application.
NestJS
Need a hint?

The class should already be exported with the export keyword before class.