Dependency injection basics
📖 Scenario: You are building a simple NestJS service that provides a greeting message. You want to use dependency injection to keep your code clean and testable.
🎯 Goal: Create a NestJS service called GreetingService that returns a greeting message. Then inject this service into a controller called GreetingController to return the greeting when a GET request is made.
📋 What You'll Learn
Create a service class named
GreetingService with a method getGreeting() that returns the string 'Hello, NestJS!'.Create a controller class named
GreetingController.Inject
GreetingService into GreetingController using constructor injection.Add a GET route
/greet in GreetingController that returns the greeting from GreetingService.💡 Why This Matters
🌍 Real World
Dependency injection helps keep code modular and testable by letting NestJS manage service creation and sharing.
💼 Career
Understanding dependency injection is essential for building scalable and maintainable backend applications with NestJS.
Progress0 / 4 steps