Introduction to NestJS
NestJS (Nest) is a progressive framework for building efficient, scalable, and reliable server-side applications on Node.js. It uses TypeScript as its primary language and is inspired by the Angular architecture. NestJS provides a ready-made application architecture that helps developers create well-structured, testable, and maintainable code.
The main goal of NestJS is to solve the problem of the lack of a clear architecture in the Node.js ecosystem. Unlike Express or Fastify, which are minimalistic and do not impose a structure, NestJS offers ready-made abstractions: modules, controllers, services, providers, and middleware. This is especially useful for large enterprise projects where code predictability and standardization are important.
NestJS is built on top of HTTP frameworks (Express by default, but Fastify can be used) and adds a powerful Dependency Injection system, decorators, Guards, Interceptors, Pipes, and Filters. The framework actively uses TypeScript decorators, making the code declarative and readable.
Installing NestJS
To install NestJS, you will need Node.js (version 16 or higher) and a package manager npm (or yarn, pnpm).
The easiest way to start is to use the Nest CLI:
npm install -g @nestjs/clinest new my-nest-projectcd my-nest-projectnpm run start:devIf you prefer to install NestJS in an existing project:
npm install @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs typescript @types/nodeYou will also need to install TypeScript and ts-node for development:
npm install -D typescript @types/node ts-nodeImportant: NestJS uses the reflect-metadata library for working with decorators. Make sure it is imported in the main main.ts file:
import "reflect-metadata";Quick Start with Code Example
Let's create a simple REST API application with one endpoint that returns a greeting.
Project structure:
src/├── app.controller.ts├── app.module.ts├── app.service.ts└── main.tsmain.ts — entry point:
import { NestFactory } from '@nestjs/core';import { AppModule } from './app.module';
async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); console.log('Server is running on http://localhost:3000');}bootstrap();app.module.ts — root module:
import { Module } from '@nestjs/common';import { AppController } from './app.controller';import { AppService } from './app.service';
@Module({ imports: [], controllers: [AppController], providers: [AppService],})export class AppModule {}app.controller.ts — controller:
import { Controller, Get } from '@nestjs/common';import { AppService } from './app.service';
@Controller()export class AppController { constructor(private readonly appService: AppService) {}
@Get() getHello(): string { return this.appService.getHello(); }}app.service.ts — service (business logic):
import { Injectable } from '@nestjs/common';
@Injectable()export class AppService { getHello(): string { return 'Hello World!'; }}Run the application with the command npm run start:dev and open http://localhost:3000. You will see "Hello World!".
Main Methods and Functions of NestJS
Let's look at 10 key elements of NestJS that are used in every project.
1. @Module() — module decorator
What it does: Defines a cl