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).
- 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
- 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
| 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 |
- Basic building blocks of NestJS applications
- Organize related functionality
- Support for nested modules
- Command:
nest g mo moduleName
- Handle incoming requests and return responses
- Define routes and endpoints
- Command:
nest g co controllerName
- Business logic and data access
- Can be injected into controllers or other services
- Command:
nest g s serviceName
- Any class that can be injected as dependency
- Services, repositories, factories, helpers, etc.
- Determine whether request should be handled by route handler
- Authentication and authorization
- Command:
nest g gu guardName
- Transform the result returned from a function
- Extend functionality before/after method execution
- Command:
nest g interceptor interceptorName
- Transform input data and validate it
- Data transformation and validation
- Command:
nest g pipe pipeName
- Functions executed before route handlers
- Access to request/response objects
- Command:
nest g middleware middlewareName
- ๐ท 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
npm i -g @nestjs/clinest --versionnest new app1Package Manager Options:
- npm
- yarn
- pnpm
// Create a module
nest g mo moduleName
// Example: User module
nest g mo usersModule Structure:
- Can create n-level nested modules
- Each module can import other modules
- Modules provide encapsulation and organization
nest g co controllerNamenest g s serviceName# Generate complete CRUD module
nest g res resourceName
# Options available:
# REST API
# GraphQL (code first)
# GraphQL (schema first)
# Microservice (non-HTTP)
# WebSocket gateway# Create new NestJS project
nest new project-name
# Check NestJS CLI version
nest --version
# Get help with all available commands
nest --help# Create a new module
nest g mo module-name
# Example: Create users module
nest g mo users# Create a new controller
nest g co controller-name
# Example: Create users controller
nest g co users# Create a new service
nest g s service-name
# Example: Create users service
nest g s users# Generate complete CRUD resource (module + controller + service)
nest g res resource-name
# Example: Generate complete users resource
nest g res users# 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# 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 testsrc/
โโโ main.ts # Application entry point
โโโ app.module.ts # Root module
โโโ users/ # Feature module
โ โโโ users.module.ts
โ โโโ users.controller.ts
โ โโโ users.service.ts
โโโ ...
This project is licensed under the MIT License - see the LICENSE file for details.
Happy Coding with NestJS! ๐