Nest.js is a powerful framework for building scalable and modular web applications using Node.js. One of the key features of Nest.js is its support for modules, which allow you to organize your application's code into logical units that can be easily reused and tested.
In this tutorial, we will explore the basics of creating and using modules in Nest.js with TypeScript. We will cover how to define a module, how to use it to provide services and controllers, and how to import and export modules to create a modular architecture for your application.
What are Modules in Nest.js?
In Nest.js, a module is a logical unit of code that encapsulates a set of related features. A module can contain one or more providers, controllers, and other related components that work together to provide a specific functionality.
Modules in Nest.js serve as building blocks for your application, and they provide a way to organize your code into smaller, reusable components that can be tested and maintained separately.
Creating a Module in Nest.js
To create a new module in Nest.js, you need to create a TypeScript file that exports a @Module() decorator. Here is an example of a simple module that defines a single provider:
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
@Module({
providers: [CatsService],
})
export class CatsModule {}In this example, we define a CatsModule that provides a CatsService provider. The @Module() decorator takes an object as an argument that defines the module's properties, such as its providers and controllers.
Providing Services in a Module
Services are a key component of most Nest.js applications, and they are typically defined within a module. To provide a service in a module, you can define a provider class that implements the service's functionality.
Here is an example of a CatsService provider that provides a simple method for retrieving a list of cats:
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {
private readonly cats: string[] = ['Garfield', 'Tom', 'Sylvester'];
findAll(): string[] {
return this.cats;
}
}In this example, we define a CatsService class that is decorated with the @Injectable() decorator. This decorator tells Nest.js that this class can be injected into other components within the application.
Using Controllers in a Module
Controllers are another key component of Nest.js applications, and they are responsible for handling incoming HTTP requests and returning responses. Controllers are typically defined within a module, and they can use services that are provided by other modules.
Here is an example of a CatsController that uses the CatsService provider that we defined earlier:
import { Controller, Get } from '@nestjs/common';
import { CatsService } from './cats.service';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get()
findAll(): string[] {
return this.catsService.findAll();
}
}
In this example, we define a CatsController class that is decorated with the @Controller() decorator. This decorator tells Nest.js that this class is a controller that should handle HTTP requests for the /cats route.
The CatsController constructor takes a CatsService instance as a parameter, which is provided by the CatsModule. The findAll() method uses the CatsService instance to retrieve a list of cats and return it as an HTTP response.
Importing and Exporting Modules
To use a module in another module, you need to import it into the module that requires it. In Nest.js, you can import modules using the imports property of the @Module() decorator.
Here is an example of importing the CatsModule into another module:
import { Module } from '@nestjs/common';
import { CatsModule } from './cats/cats.module';
import { DogsController } from './dogs.controller';
@Module({
imports: [CatsModule],
controllers: [DogsController],
})
export class AppModule {}In this example, we define an AppModule that imports the CatsModule and provides a DogsController. The DogsController can use the CatsService that is provided by the CatsModule to retrieve and manipulate data.
You can also export modules from a module using the exports property of the @Module() decorator. Exported modules can be used by other modules that import the current module.
Here is an example of exporting the CatsModule from the CatsModule itself:
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
@Module({
providers: [CatsService],
controllers: [CatsController],
exports: [CatsService],
})
export class CatsModule {}In this example, we define a CatsModule that provides a CatsService and a CatsController. We also export the CatsService provider so that it can be used by other modules that import the CatsModule.
Conclusion
In this tutorial, we have explored the basics of creating and using modules in Nest.js with TypeScript. We have covered how to define a module, how to use it to provide services and controllers, and how to import and export modules to create a modular architecture for your application.
Modules in Nest.js provide a powerful way to organize your code into smaller, reusable components that can be tested and maintained separately. By using modules, you can create a more scalable and maintainable application that can be easily extended and customized as your needs evolve.