{"library":"nestjs","title":"NestJS - Progressive Node.js Framework","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nestjs"],"cli":null},"imports":["import { NestFactory } from '@nestjs/core';","import { Module } from '@nestjs/common';","import { Controller, Get, Post } from '@nestjs/common';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { NestFactory } from '@nestjs/core';\nimport { Module, Controller, Get, Post, Body, Param, HttpStatus, HttpException, Injectable } from '@nestjs/common';\n\n// 1. Define a Service (Provider)\n@Injectable()\nclass AppService {\n  private items: { id: number; name: string }[] = [];\n  constructor() {\n    this.items.push({ id: 1, name: 'Item 1' });\n  }\n\n  getAllItems(): { id: number; name: string }[] {\n    return this.items;\n  }\n\n  getItemById(id: number): { id: number; name: string } | undefined {\n    return this.items.find(item => item.id === id);\n  }\n\n  createItem(name: string): { id: number; name: string } {\n    const newItem = { id: Date.now(), name };\n    this.items.push(newItem);\n    return newItem;\n  }\n}\n\n// 2. Define a Controller\n@Controller('items')\nclass AppController {\n  constructor(private readonly appService: AppService) {}\n\n  @Get()\n  getAllItems(): { id: number; name: string }[] {\n    return this.appService.getAllItems();\n  }\n\n  @Get(':id')\n  getItem(@Param('id') id: string): { id: number; name: string } {\n    const foundItem = this.appService.getItemById(parseInt(id, 10));\n    if (!foundItem) {\n      throw new HttpException('Item not found', HttpStatus.NOT_FOUND);\n    }\n    return foundItem;\n  }\n\n  @Post()\n  createItem(@Body('name') name: string): { id: number; name: string } {\n    if (!name) {\n      throw new HttpException('Name is required', HttpStatus.BAD_REQUEST);\n    }\n    return this.appService.createItem(name);\n  }\n}\n\n// 3. Define the Root Module\n@Module({\n  imports: [],\n  controllers: [AppController],\n  providers: [AppService],\n})\nclass AppModule {}\n\n// 4. Bootstrap the Application\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n  await app.listen(process.env.PORT ?? 3000);\n  console.log(`Application is running on: http://localhost:${process.env.PORT ?? 3000}`);\n}\n\nbootstrap();","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}