Updated on 16 Dec, 202510 mins read 29 views

NestJS Is an Orchestration Layer

At runtime, a NestJS application is:

  • A Node.js process
  • Running an HTTP server (Express or Fastify)
  • With a Dependency Injection Container
  • And a module graph describing the architecture

NestJS does not replace Node.js. NestJS coordinates your application components.

Think of NestJS as:

Spring Boot-like orchestration for Node.js

The True Entry Point: main.ts

Every NestJS application starts with main.ts:

async function bootstrap() {
	const app = await NestFactory.create(AppModule);
	await app.listen(3000);
}
bootstrap();

What main.ts Should Do

  • Create the application
  • Configure global concerns
  • Start listening

What main.ts Must NOT Do

  • Business logic
  • Database queries
  • Feature wiring

What Really Happens Inside NestFactory.create()

This single call triggers the entire framework lifecycle.

High-Level Phases

  1. Create application context
  2. Initialize DI container
  3. Scan and resolve modules
  4. Register providers
  5. Instantiate controllers
  6. Bind routes
  7. Prepare HTTP adapter

No HTTP request is accepted before all of this completes.

This guarantess deterministic startup.

app.module.ts – The Blueprint

This file organizes your app. Think of it as your home's blueprint:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],       // Other blueprints you're using
  controllers: [AppController], // People who answer the door
  providers: [AppService],     // People who do the actual work
})
export class AppModule {}

app.controller.ts – The Receptionist

Controllers handle incoming requests. They are like receptionists who decide who handles each visitor:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller() // This makes it a controller
export class AppController {
  constructor(private readonly appService: AppService) {}
  
  @Get() // When someone visits the homepage...
  getHello(): string {
    return this.appService.getHello(); // Ask the service to handle it
  }
}

app.service.ts –  The Worker

Services contain your business logic. They are the workers who actually get things done:

import { Injectable } from '@nestjs/common';

@Injectable() // This marks it as a service
export class AppService {
  getHello(): string {
    return 'Hello World!'; // The actual work
  }
}

The Magic Connection: How Everything Works Together

Here's the simple flow:

  1. Someone visits http://localhost:3000
  2. main.ts starts the app
  3. app.module.ts organizes who does what
  4. app.controller.ts receives the request
  5. app.service.ts does the actual work
  6. The response goes back to the visitor

 

Buy Me A Coffee

Leave a comment

Your email address will not be published. Required fields are marked *