๐Ÿ“˜ NestJS Blog Series โ€“ Series 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    ๐Ÿ“˜ 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

    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, 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 requests
    • providers: Services used by the controller or other providers
    • imports: 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?




    More...
Working...