basics
SKILL.md
NestJS Basics
When to Use This Skill
Use this skill when:
- Setting up a new NestJS project
- Installing and configuring NestJS CLI
- Understanding NestJS core concepts and architecture
- Bootstrapping an application
- Explaining NestJS to beginners
Installation
Install NestJS CLI Globally
npm i -g @nestjs/cli
Create New Project
nest new project-name
The CLI will prompt you to choose a package manager (npm, yarn, or pnpm).
Requirements
- Node.js version 20 or higher
- TypeScript 5.x recommended
Core Concepts
What is NestJS?
NestJS is a progressive Node.js framework for building efficient, scalable server-side applications. Key features:
- TypeScript-first - Full TypeScript support with strong typing
- Platform-agnostic - Works with Express (default) or Fastify
- Modular architecture - Inspired by Angular's module system
- Dependency Injection - Built-in IoC container
- Decorator-based - Uses decorators for clean, declarative code
- Testing-friendly - Built with testability in mind
Project Structure
Generated projects include:
src/
├── app.controller.ts # Basic controller with a single route
├── app.controller.spec.ts # Unit tests for the controller
├── app.module.ts # Root module
├── app.service.ts # Basic service
└── main.ts # Entry point - bootstraps the application
Bootstrap Pattern
The main.ts file is 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();
Key Points:
NestFactory.create()creates the application instance- Accepts the root module as a parameter
- Returns a
NestApplicationinstance - Use
app.listen()to start the HTTP server
Platform Selection
NestJS supports two HTTP platforms:
Express (default):
const app = await NestFactory.create(AppModule);
Fastify (for better performance):
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
Running the Application
Development Mode (with watch)
npm run start:dev
Production Mode
npm run build
npm run start:prod
Debug Mode
npm run start:debug
Core Building Blocks
Every NestJS application consists of:
- Modules - Organize application structure (
@Module()) - Controllers - Handle requests and responses (
@Controller()) - Providers - Business logic and services (
@Injectable()) - Middleware - Request preprocessing
- Guards - Authorization and route protection
- Interceptors - Transform requests/responses
- Pipes - Validation and transformation
- Exception Filters - Error handling
Request Processing Pipeline
Incoming Request
↓
Middleware
↓
Guards
↓
Interceptors (before)
↓
Pipes
↓
Route Handler (Controller)
↓
Interceptors (after)
↓
Exception Filters
↓
Response
Architecture Philosophy
NestJS follows these principles:
- Modularity - Organize code into feature modules
- Single Responsibility - Each class has one clear purpose
- Dependency Injection - Loose coupling through DI
- Separation of Concerns - Controllers handle routing, services handle business logic
- Testability - Built to support unit and e2e testing
Common CLI Commands
# Generate a module
nest generate module users
# Generate a controller
nest generate controller users
# Generate a service
nest generate service users
# Generate a complete resource (module, controller, service, DTO)
nest generate resource users
# View all available commands
nest --help
Best Practices
- Use TypeScript - Leverage strong typing for better DX
- Follow module structure - One feature per module
- Use DTOs - Define data transfer objects with validation
- Dependency injection - Inject dependencies via constructors
- Error handling - Use built-in exception classes
- Configuration - Use
@nestjs/configfor environment variables - Validation - Use
class-validatorwithValidationPipe
Next Steps
After setting up your project:
- Learn about Controllers for handling routes
- Learn about Providers for business logic
- Learn about Modules for organizing your application
- Set up Validation for request data
- Implement Authentication and Authorization
Weekly Installs
1
Repository
ramziddin/ccpluginsGitHub Stars
1
First Seen
6 days ago
Security Audits
Installed on
amp1
cline1
opencode1
cursor1
kimi-cli1
warp1