NestJS for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

What is NestJS and why is it needed?

\n

NestJS 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.

\n

Why 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).

\n

The 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\n

Installation

\n

To install NestJS, use the npm (or yarn) package manager. It is recommended to install the CLI tool globally for quick project creation:

\n
npm install -g @nestjs/cli
\n

After installing the CLI, you can create a new project with the command:

\n
nest 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:

\n
npm install @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
\n

Note: the reflect-metadata library is required for decorators to work.

\n\n

Quick Start: Minimal Working Example

\n

Let's create a simple REST API with one endpoint returning "Hello World".

\n

Step 1: Create the file app.controller.js

\n
// 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

\n
// 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

\n
// 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

\n
node main.js
\n

Now navigate to http://localhost:3000 — you will see "Hello World!".

\n\n

Main Methods, Classes, and Decorators

\n

Below are 8 key elements

Recommendations