Node.js Framework Series — 1.2.2. NestJS — Modules

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { LoginController } from './login/login.controller';
import { RegisterController } from './register/register.controller';
import { ProfileController } from './profile/profile.controller';
@Module({
imports: [],
controllers: [AppController, LoginController, RegisterController, ProfileController],
providers: [AppService],
})
export class AppModule {}
NestJS CLI Tool — Object creation sequence
import { Module } from '@nestjs/common';
import { NotificationController } from './notification.controller';
import { NotificationService } from './notification.service';
@Module({
controllers: [NotificationController],
providers: [NotificationService]
})
export class NotificationModule {}
@Module({
imports: [NotificationModule],
controllers: [AppController, LoginController, RegisterController, ProfileController],
providers: [AppService],
})
export class AppModule {}

Feature modules

Shared Module

import { Module } from '@nestjs/common';
import { NotificationController } from './notification.controller';
import { NotificationService } from './notification.service';
@Module({
controllers: [NotificationController],
providers: [NotificationService],
exports: [NotificationService],
})
export class NotificationModule {}

Module re-exporting

@Module({
imports: [CommonModule],
exports: [CommonModule],
})
export class CoreModule {}

Dependency Injection[3]

import { Module } from '@nestjs/common';
import { NotificationController } from './notification.controller';
import { NotificationService } from './notification.service';
@Module({
controllers: [NotificationController],
providers: [NotificationService],
})
export class NotificationModule {
constructor(private notificationService: NotificationService) {}
}

Global modules

import { Module, Global } from '@nestjs/common';
import { NotificationController } from './notification.controller';
import { NotificationService } from './notification.service';
@Global()
@Module({
controllers: [NotificationController],
providers: [NotificationService],
exports: [NotificationService],
})
export class NotificationModule {}

Technical Training Coordinator at www.Zero2Hero.Training, Blockchain Developer(Oxford BC Strategy Programme Certified) and an AWS Certified Solution Architect.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store