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
- Create application context
- Initialize DI container
- Scan and resolve modules
- Register providers
- Instantiate controllers
- Bind routes
- 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:
- Someone visits
http://localhost:3000 main.tsstarts the appapp.module.tsorganizes who does whatapp.controller.tsreceives the requestapp.service.tsdoes the actual work- The response goes back to the visitor
Leave a comment
Your email address will not be published. Required fields are marked *
