Modules in NestJS
Share:
In this tutorial, we're going to dive into the modular structure of NestJS by understanding how NestJS leverages the concept of modules to effectively organize and structure your application’s components. We will be using examples related to movie features and characters to make things easier to comprehend.
In any non-trivial application, the codebase eventually grows big enough that it could become difficult to manage and maintain if the code is not well-structured. This is where NestJS modules come into play, they help highly enhance maintainability and scalability of your application by assisting in code organization.
What is a NestJS Module?
In simple terms, a NestJS module is a class annotated with a @Module()
decorator. The @Module()
decorator provides metadata that NestJS uses to organize the application structure.
Here's a basic example of a NestJS module:
import { Module } from '@nestjs/common';
@Module({})
export class MovieModule {}
The @Module()
decorator takes an object with several properties to configure the module.
- providers: This array hosts the injectable providers for this module, which can be shared across the whole application.
- controllers: They define the routes that can be handled by this module.
- imports: List of modules that this module depends on.
- exports: The set of providers that should be available to other modules.
Let's create a module for a Movies application to understand the use of these properties:
import { Module } from '@nestjs/common';
import { MoviesService } from './movies.service';
import { MoviesController } from './movies.controller';
@Module({
controllers: [MoviesController],
providers: [MoviesService],
})
export class MoviesModule {}
Here, the MoviesModule
is making use of MoviesService
as a provider and MoviesController
as a controller. Essentially, this module is saying that when a request comes in for handling a movie-related route, it should be handled by MoviesController
which could then make use of MoviesService
to perform any required business logic.
NestJS Module Dependency Injection
Importing modules allows us to make use of its exported content such as providers. Let's assume we have another CharactersModule
which exports a CharactersService
.
import { Module } from '@nestjs/common';
import { CharactersService } from './characters.service';
@Module({
providers: [CharactersService],
exports: [CharactersService]
})
export class CharactersModule {}
We can make the CharactersService
available in the MoviesModule
by importing the CharactersModule
:
import { Module } from '@nestjs/common';
import { MoviesService } from './movies.service';
import { MoviesController } from './movies.controller';
import { CharactersModule } from '../characters/characters.module';
@Module({
imports: [CharactersModule],
controllers: [MoviesController],
providers: [MoviesService],
})
export class MoviesModule {}
This allows other modules (like the MoviesModule
) to have access to the CharactersService
. This demonstrates the idea of modularity and reusability of services across different modules.
Shared Modules
In NestJS, a module that imports another module is often considered to be a child of that module. However, there are instances when we want a module to be a global or shared module. To accomplish this, we use the @Global()
decorator in conjunction with the @Module()
decorator:
import { Global, Module } from '@nestjs/common';
import { ConfigurationService } from './configuration.service';
@Global()
@Module({
providers: [ConfigurationService],
exports: [ConfigurationService],
})
export class ConfigurationModule {}
Any module that is annotated with @Global()
becomes a singleton and can be imported across your entire application. Its providers will be the same instances anywhere they are injected.
With this, we have covered a decent introduction to NestJS modules. You should now have a good understanding of how to create modules, how dependency between modules work, and the concept of shared modules.
In the next part of our tutorial series, we will learn about middleware in NestJS and how they play a vital role in handling incoming client requests and inject additional functionality in your application.
Remember, the key to mastering any framework lies in understanding how to structure your code effectively and NestJS modules are a solid way of achieving that. By understanding and using modules correctly, you can write clean, maintainable, and scalable code.
0 Comment
Sign up or Log in to leave a comment