Using the Injectable Decorator in NestJS
📖 Scenario: You are building a simple NestJS service to manage a list of books in a library. You want to make this service available for other parts of your application by marking it as injectable.
🎯 Goal: Create a NestJS service class called BooksService and use the @Injectable() decorator to make it injectable in other parts of the app.
📋 What You'll Learn
Create a class named
BooksServiceUse the
@Injectable() decorator from @nestjs/commonDefine a property
books as an array of strings with these exact values: '1984', 'Brave New World', 'Fahrenheit 451'Add a method
getBooks() that returns the books array💡 Why This Matters
🌍 Real World
Services in NestJS are used to hold business logic and data. Marking them with @Injectable() allows NestJS to manage their lifecycle and inject them where needed.
💼 Career
Understanding how to create injectable services is essential for building scalable and maintainable backend applications with NestJS, a popular Node.js framework.
Progress0 / 4 steps