Skip to content

Ashukr321/Complete_Nest_Js_2k25_Basic_To_Advanced

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NestJS - Complete Guide

NestJS Logo

๐Ÿš€ What is NestJS?

NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. It uses modern JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

๐Ÿ“š Learning RoadMap

Day 1 - NestJS Fundamentals

  • Why NestJS? - Understanding the benefits and use cases
  • Nest App from Scratch - Setting up your first NestJS application
  • Modules - Understanding the modular architecture
  • Controllers - Handling HTTP requests and responses
  • Providers - Dependency injection and service management
  • Environment Configuration - Managing app settings and secrets

Day 2 - Middlewares in the Nest js

  • How to add middleware in the nest js
  • Ways to add middleware in the nest js
  • functional based approach
  • class based approach
  • apply middleware on specific modules

๐ŸŽฏ Problems NestJS Solves Over Express.js

Feature Express.js NestJS
Architecture Minimalist, unopinionated Opinionated, structured
TypeScript Support Manual setup required Built-in TypeScript support
Dependency Injection Manual implementation Built-in DI container
Modular Architecture Manual organization Built-in module system
Testing Manual setup Built-in testing utilities
Code Organization Developer's choice Enforced structure
Scalability Requires careful planning Built for enterprise apps
Documentation Community-driven Auto-generated OpenAPI/Swagger

๐Ÿ—๏ธ Core Components of NestJS

1. Modules (@Module())

  • Basic building blocks of NestJS applications
  • Organize related functionality
  • Support for nested modules
  • Command: nest g mo moduleName

2. Controllers (@Controller())

  • Handle incoming requests and return responses
  • Define routes and endpoints
  • Command: nest g co controllerName

3. Services (@Injectable())

  • Business logic and data access
  • Can be injected into controllers or other services
  • Command: nest g s serviceName

4. Providers (@Injectable())

  • Any class that can be injected as dependency
  • Services, repositories, factories, helpers, etc.

5. Guards (@Injectable())

  • Determine whether request should be handled by route handler
  • Authentication and authorization
  • Command: nest g gu guardName

6. Interceptors (@Injectable())

  • Transform the result returned from a function
  • Extend functionality before/after method execution
  • Command: nest g interceptor interceptorName

7. Pipes (@Injectable())

  • Transform input data and validate it
  • Data transformation and validation
  • Command: nest g pipe pipeName

8. Middleware (@Injectable())

  • Functions executed before route handlers
  • Access to request/response objects
  • Command: nest g middleware middlewareName

โœจ Key Features of NestJS

  • ๐Ÿ”ท Built with TypeScript - Full TypeScript support out of the box
  • ๐Ÿ›๏ธ Modular Architecture - Organize code into modules for better maintainability
  • ๐Ÿ’‰ Dependency Injection - Built-in DI container for better testability
  • ๐Ÿ›ก๏ธ Guards & Interceptors - Built-in authentication and request/response transformation
  • ๐Ÿ“š Auto Documentation - Auto-generate OpenAPI/Swagger documentation
  • ๐Ÿงช Testing Support - Built-in testing utilities and mocking
  • ๐Ÿš€ Performance - Built on Express.js or Fastify for high performance
  • ๐Ÿ“ฆ Microservices Ready - Built-in support for microservices architecture
  • ๐Ÿ”Œ WebSocket Support - Real-time applications with WebSocket support
  • ๐ŸŒ GraphQL Support - Built-in GraphQL support with code-first approach

๐Ÿ› ๏ธ Installation & Setup

1. Install NestJS CLI globally

npm i -g @nestjs/cli

2. Check NestJS version

nest --version

3. Create a new NestJS project

nest new app1

Package Manager Options:

  • npm
  • yarn
  • pnpm

๐Ÿ“š Important Concepts

1. Modules - Nested Module System

// Create a module
nest g mo moduleName

// Example: User module
nest g mo users

Module Structure:

  • Can create n-level nested modules
  • Each module can import other modules
  • Modules provide encapsulation and organization

2. Controllers - Handle HTTP Requests

nest g co controllerName

3. Services - Business Logic

nest g s serviceName

4. Complete CRUD Generation

# Generate complete CRUD module
nest g res resourceName

# Options available:
# REST API
# GraphQL (code first)
# GraphQL (schema first)
# Microservice (non-HTTP)
# WebSocket gateway

โšก NestJS CLI Shortcuts & Commands

๐Ÿ—๏ธ Project Creation

# Create new NestJS project
nest new project-name

# Check NestJS CLI version
nest --version

# Get help with all available commands
nest --help

๐Ÿ“ฆ Module Commands

# Create a new module
nest g mo module-name

# Example: Create users module
nest g mo users

๐ŸŽฎ Controller Commands

# Create a new controller
nest g co controller-name

# Example: Create users controller
nest g co users

๐Ÿ› ๏ธ Service Commands

# Create a new service
nest g s service-name

# Example: Create users service
nest g s users

๐Ÿš€ Complete Resource Generation

# Generate complete CRUD resource (module + controller + service)
nest g res resource-name

# Example: Generate complete users resource
nest g res users

๐Ÿ”ง Other Useful Commands

# Generate guard
nest g gu guard-name

# Generate interceptor
nest g interceptor interceptor-name

# Generate pipe
nest g pipe pipe-name

# Generate middleware
nest g middleware middleware-name

# Generate decorator
nest g decorator decorator-name

# Generate filter
nest g filter filter-name

# Generate gateway (WebSocket)
nest g gateway gateway-name

๐Ÿš€ Quick Start Commands

# Create new project
nest new my-app

# Generate module
nest g mo feature-name

# Generate controller
nest g co feature-name

# Generate service
nest g s feature-name

# Generate complete resource
nest g res feature-name

# Start development server
npm run start:dev

# Build project
npm run build

# Run tests
npm run test

๐Ÿ“ Project Structure

src/
โ”œโ”€โ”€ main.ts              # Application entry point
โ”œโ”€โ”€ app.module.ts        # Root module
โ”œโ”€โ”€ users/               # Feature module
โ”‚   โ”œโ”€โ”€ users.module.ts
โ”‚   โ”œโ”€โ”€ users.controller.ts
โ”‚   โ””โ”€โ”€ users.service.ts
โ””โ”€โ”€ ...

๐Ÿค Contributors

  • Ashutosh Kumar - Full Stack Developer & Project Maintainer - GitHub | Portfolio

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ”— Useful Links


Happy Coding with NestJS! ๐ŸŽ‰

About

NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. It uses modern JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors