Getting Started with NestJS: A Beginner-Friendly Tutorial

Learn the fundamentals of NestJS, a progressive Node.js framework, with this easy-to-follow tutorial. This guide covers installation, project setup, and building a basic application, providing a solid foundation for developing efficient and scalable server-side applications using TypeScript or JavaScript.



Getting Started with NestJS: A Beginner's Tutorial

Introduction

NestJS is a popular and versatile Node.js framework for building efficient and scalable server-side applications. This tutorial provides a beginner-friendly introduction, guiding you through installation, project setup, and a basic example.

What is NestJS?

NestJS is a progressive Node.js framework built with TypeScript (but you can also use JavaScript). It leverages the strengths of Node.js and uses a structure inspired by Angular, making it well-suited for building complex and maintainable server-side applications. NestJS supports OOP (Object-Oriented Programming), FRP (Functional Reactive Programming), and FP (Functional Programming) paradigms.

NestJS Architecture

NestJS builds on top of Express.js (by default) or Fastify (optionally), providing a layer of abstraction. This means you interact with a cleaner API, while NestJS manages the underlying complexities of the HTTP server framework. This abstraction makes it easier to use third-party libraries and to maintain a clear separation of concerns.

Installing NestJS

  1. Install Node.js and npm: If not already installed, download and install Node.js from https://nodejs.org/. npm is included with Node.js.
  2. Install Nest CLI: Use npm to install the Nest CLI globally:
  3. Installing Nest CLI
    
    npm i -g @nestjs/cli
    
  4. Create a New Project: Create a new NestJS project using the Nest CLI:
  5. Creating a Project
    
    nest new my-nestjs-01
    

This creates a new directory named "my-nestjs-01" containing the project files.

Project Structure and Core Files

The generated project includes several key files:

  • src folder: Contains the main application code (TypeScript files).
  • main.ts: The application's entry point.
  • app.module.ts: Defines the application's modules.
  • app.controller.ts: A controller class handling HTTP requests.
  • app.service.ts: A service class providing business logic.
  • app.controller.spec.ts: A test file for the controller.
NestJS Project Structure Diagram

main.ts

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

app.module.ts

app.module.ts

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

app.controller.ts

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

app.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}

Conclusion

This tutorial provided a foundational understanding of NestJS setup and structure. This is a starting point for building more complex applications leveraging NestJS's features and capabilities.