๐ NestJS Blog Series โ Series 1


๐ ๏ธ Bootstrapping a NestJS App & Understanding App Structure
โ
Getting Started with nest new todoApp
We create a new NestJS project using the CLI:
nest new todoApp
This sets up a clean TypeScript backend with:
A modular structure
TypeScript out of the box
Preconfigured build and test tools
๐ Project Structure Overview
After generating the app, youโll see this:
src/
โโโ app.controller.ts
โโโ app.module.ts
โโโ app.service.ts
โโโ main.ts
๐ main.ts
โ The Entry Point
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
๐ What is NestFactory
?
NestFactory
is a helper class that bootstraps the NestJS application.Under the hood, it sets up the HTTP server (based on Express by default).
You can swap it for Fastify if needed.
const app = await NestFactory.create(AppModule);
This creates an instance of the application using the root module AppModule
.
๐ก NestFactory vs Express
Feature | Express | NestJS (with NestFactory) |
Architecture | Unopinionated, DIY | Modular, opinionated |
Dependency Injection | Manual | Automatic |
TypeScript Support | Optional | Built-in |
CLI Tools | None | Powerful scaffolding via Nest CLI |
Testing | Manual | Built-in tools and patterns |
NestJS builds on top of Express, giving you a framework that's structured, testable, and scalable.
๐งฑ AppModule
โ The Root Module
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HelloModule } from './hello/hello.module';
@Module({
imports: [HelloModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
๐งฉ Whatโs a Module in NestJS?
A module is a class annotated with
@Module()
It organizes code into cohesive blocks: controllers, services, and other modules
๐งฉ A Feature Module Example โ HelloModule
import { Module } from '@nestjs/common';
import { HelloController } from './hello.controller';
import { HelloService } from './hello.service';
@Module({
controllers: [HelloController],
providers: [HelloService],
imports: [],
exports: []
})
export class HelloModule {}
๐งญ Explanation
controllers
: Handle HTTP requestsproviders
: Services used by the controller or other providersimports
: Import other modules (e.g., DatabaseModule)exports
: Expose services to other modules if needed
๐ฎ Controller โ Handling HTTP Requests
import { Controller, Get, Param, Query } from '@nestjs/common';
import { HelloService } from './hello.service';
@Controller('hello')
export class HelloController {
constructor(private readonly helloService: HelloService) {}
@Get()
getHello(): string {
return this.helloService.getHello();
}
@Get("/name/:username")
getUser(@Param("username") username: string): string {
return this.helloService.getHelloWithName(username);
}
@Get('query')
getHelloUser(@Query("name") name: string): string {
return this.helloService.getHelloWithName(name);
}
}
๐ Key Concepts
@Controller('hello')
: All routes start with/hello
@Get()
: Handles GET request to/hello
@Param()
: Gets URL parameter@Query()
: Gets query string param like/hello/query?name=John
๐ Constructor Injection
constructor(private readonly helloService: HelloService) {}
This uses NestJS Dependency Injection to inject the HelloService
. Itโs how services are shared in NestJS.
โ๏ธ Service โ Business Logic Layer
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloService {
getHello(): string {
return 'Hello nestjs';
}
getHelloWithName(name: string): string {
return `Hello ${name} from nestjs`;
}
}
๐ก What is @Injectable()
?
This tells NestJS that this class can be injected into other classes.
Think of it like
@Service
in Spring or a provider in Angular.
Would you like me to generate Series 2 now with a fully working TodoModule
+ DTO + in-memory storage?
Subscribe to my newsletter
Read articles from CHIRAG KUMAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

CHIRAG KUMAR
CHIRAG KUMAR
Full stack Developer