NestJS - Progressive Node.js Framework

0.0.1 · active · verified Sun Apr 19

NestJS is a progressive, open-source Node.js framework for building efficient, reliable, and scalable server-side applications, released under an MIT License. It leverages TypeScript extensively, combining elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP) to provide a structured and opinionated development experience. Built on top of robust HTTP server frameworks like Express (default) and Fastify, it provides an out-of-the-box application architecture that helps developers create highly testable, loosely coupled, and easily maintainable applications. The current stable version is 11.1.19, with regular releases bringing new features, bug fixes, and performance enhancements. Key differentiators include its strong adherence to Angular-inspired modularity, dependency injection system, and a rich ecosystem for various application types like REST APIs, GraphQL APIs, and microservices.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic NestJS application with a service, controller, and module, showcasing dependency injection and routing. It creates a simple CRUD API for 'items', listening on port 3000.

import { NestFactory } from '@nestjs/core';
import { Module, Controller, Get, Post, Body, Param, HttpStatus, HttpException, Injectable } from '@nestjs/common';

// 1. Define a Service (Provider)
@Injectable()
class AppService {
  private items: { id: number; name: string }[] = [];
  constructor() {
    this.items.push({ id: 1, name: 'Item 1' });
  }

  getAllItems(): { id: number; name: string }[] {
    return this.items;
  }

  getItemById(id: number): { id: number; name: string } | undefined {
    return this.items.find(item => item.id === id);
  }

  createItem(name: string): { id: number; name: string } {
    const newItem = { id: Date.now(), name };
    this.items.push(newItem);
    return newItem;
  }
}

// 2. Define a Controller
@Controller('items')
class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getAllItems(): { id: number; name: string }[] {
    return this.appService.getAllItems();
  }

  @Get(':id')
  getItem(@Param('id') id: string): { id: number; name: string } {
    const foundItem = this.appService.getItemById(parseInt(id, 10));
    if (!foundItem) {
      throw new HttpException('Item not found', HttpStatus.NOT_FOUND);
    }
    return foundItem;
  }

  @Post()
  createItem(@Body('name') name: string): { id: number; name: string } {
    if (!name) {
      throw new HttpException('Name is required', HttpStatus.BAD_REQUEST);
    }
    return this.appService.createItem(name);
  }
}

// 3. Define the Root Module
@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
class AppModule {}

// 4. Bootstrap the Application
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT ?? 3000);
  console.log(`Application is running on: http://localhost:${process.env.PORT ?? 3000}`);
}

bootstrap();

view raw JSON →