0
0
NestJSframework~30 mins

Service creation in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Service Creation in NestJS
📖 Scenario: You are building a simple NestJS application to manage a list of books in a library. You want to create a service that will hold the data and provide methods to access it.
🎯 Goal: Create a NestJS service called BooksService that stores a list of books and provides a method to get all books.
📋 What You'll Learn
Create a service class named BooksService
Add a private array property called books with three book titles as strings
Add a method called getAllBooks() that returns the books array
Export the service class properly
💡 Why This Matters
🌍 Real World
Services in NestJS hold business logic and data. Creating a service to manage books is like having a librarian who knows all the books and can share that info when asked.
💼 Career
Understanding how to create and use services is essential for backend development with NestJS, a popular framework for building scalable server applications.
Progress0 / 4 steps
1
Create the BooksService class with a books array
Create a class called BooksService and inside it, create a private property called books that is an array containing these exact strings: 'The Hobbit', '1984', 'Brave New World'.
NestJS
Need a hint?

Remember to use private books = [...] inside the class.

2
Add the getAllBooks method
Inside the BooksService class, add a method called getAllBooks() that returns the books array.
NestJS
Need a hint?

The method should return this.books.

3
Add the @Injectable decorator
Import Injectable from @nestjs/common and add the @Injectable() decorator above the BooksService class.
NestJS
Need a hint?

Use @Injectable() just before the class declaration.

4
Export the BooksService class
Add an export statement to export the BooksService class so it can be used in other parts of the application.
NestJS
Need a hint?

Use export class BooksService instead of just class BooksService.