What is NestJS and why is it needed?
\nNestJS is a progressive framework for building efficient, reliable, and scalable server-side applications on Node.js. It uses an expressive architecture inspired by Angular and is built on TypeScript by default, but fully supports plain JavaScript as well. NestJS abstracts low-level details of popular libraries such as Express (by default) or Fastify, providing the developer with a powerful set of tools for organizing code: modules, controllers, services, providers, middleware, guards, interceptors, pipes, and filters.
\nWhy is NestJS needed? In modern web development on Node.js, the problem of code organization often arises: simple Express scripts quickly turn into "spaghetti" as functionality grows. NestJS solves this problem by enforcing a clear modular structure and the principles of Dependency Injection. This makes testing, maintenance, and scaling of the application easier. The framework is ideal for building REST APIs, GraphQL servers, microservices, as well as real-time applications (WebSockets).
\nThe key advantage of NestJS is its ecosystem. It provides official modules for working with databases (TypeORM, Mongoose, Prisma), authentication (Passport), validation (class-validator), configuration, queues (Bull), caching, and much more. Thanks to this, the developer can focus on business logic rather than integrating disparate libraries.
\n\nInstallation
\nTo install NestJS, use the npm (or yarn) package manager. It is recommended to install the CLI tool globally for quick project creation:
\nnpm install -g @nestjs/cli\n After installing the CLI, you can create a new project with the command:
\nnest new my-nest-project\n During installation, you will be asked to choose a package manager (npm, yarn, or pnpm) and a language (TypeScript — recommended, or JavaScript). If you want to add NestJS to an existing project, install the core packages:
\nnpm install @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs\n Note: the reflect-metadata library is required for decorators to work.
Quick Start: Minimal Working Example
\nLet's create a simple REST API with one endpoint returning "Hello World".
\nStep 1: Create the file app.controller.js
// app.controller.js\nconst { Controller, Get } = require('@nestjs/common');\n\n@Controller()\nexport class AppController {\n @Get()\n getHello() {\n return 'Hello World!';\n }\n}\n Step 2: Create the file app.module.js
// app.module.js\nconst { Module } = require('@nestjs/common');\nconst { AppController } = require('./app.controller');\n\n@Module({\n controllers: [AppController],\n})\nexport class AppModule {}\n Step 3: Create the main file main.js
// main.js\nconst { NestFactory } = require('@nestjs/core');\nconst { AppModule } = require('./app.module');\n\nasync function bootstrap() {\n const app = await NestFactory.create(AppModule);\n await app.listen(3000);\n console.log('Server running at http://localhost:3000');\n}\nbootstrap();\n Step 4: Run the application
\nnode main.js\n Now navigate to http://localhost:3000 — you will see "Hello World!".
Main Methods, Classes, and Decorators
\nBelow are 8 key elements