Feature modules help organize your NestJS app by grouping related code together. This makes your app easier to understand and maintain.
0
0
Feature modules in NestJS
Introduction
When you want to separate different parts of your app, like users, products, or orders.
When your app grows and you need to keep code organized and manageable.
When you want to reuse a group of related services and controllers in different parts of your app.
When you want to load parts of your app only when needed to improve performance.
Syntax
NestJS
import { Module } from '@nestjs/common'; import { SomeService } from './some.service'; import { SomeController } from './some.controller'; @Module({ imports: [], controllers: [SomeController], providers: [SomeService], exports: [SomeService], }) export class SomeModule {}
The @Module decorator defines a feature module.
You list controllers, providers (services), and other modules inside the decorator.
Examples
This is a simple feature module for user-related code.
NestJS
import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; @Module({ controllers: [UsersController], providers: [UsersService], }) export class UsersModule {}
This module imports a database entity and exports its service for use in other modules.
NestJS
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Product } from './product.entity'; import { ProductsService } from './products.service'; import { ProductsController } from './products.controller'; @Module({ imports: [TypeOrmModule.forFeature([Product])], controllers: [ProductsController], providers: [ProductsService], exports: [ProductsService], }) export class ProductsModule {}
Sample Program
This feature module groups the cats controller and service. The controller uses the service to return a list of cat names when you visit the '/cats' route.
NestJS
import { Module } from '@nestjs/common'; import { CatsController } from './cats.controller'; import { CatsService } from './cats.service'; @Module({ controllers: [CatsController], providers: [CatsService], exports: [CatsService], }) export class CatsModule {} // cats.service.ts import { Injectable } from '@nestjs/common'; @Injectable() export class CatsService { getCats() { return ['Tom', 'Jerry', 'Garfield']; } } // cats.controller.ts import { Controller, Get } from '@nestjs/common'; import { CatsService } from './cats.service'; @Controller('cats') export class CatsController { constructor(private readonly catsService: CatsService) {} @Get() findAll() { return this.catsService.getCats(); } }
OutputSuccess
Important Notes
Feature modules help keep your app code clean and organized.
Use the exports array to share providers with other modules.
Import feature modules into your root module or other feature modules to use their functionality.
Summary
Feature modules group related controllers and services.
They make your app easier to manage and scale.
Use @Module decorator to define them.