Basic NestJS For Absolute Beginners

Date

Date

Date

April 18, 2024

April 18, 2024

April 18, 2024

Author

Author

Author

David Osuchukwu

David Osuchukwu

David Osuchukwu

Getting Started with NestJS: A Beginner-Friendly Tutorial

NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. Think of it as a backend version of Angular—structured, modular, and powerful.

If you know some basic JavaScript or TypeScript and you're comfortable with Node.js/Express, NestJS will feel like a well-organized upgrade.

🧰 Prerequisites

Before starting, make sure you have:

  • Node.js (v16 or above): https://nodejs.org

  • npm or yarn

  • Basic knowledge of JavaScript or TypeScript

  • Familiarity with REST APIs and Express is a plus

📦 Step 1: Install the Nest CLI

The Nest CLI is the easiest way to generate a new project and components.

bashCopyEditnpm i -g @nestjs/cli

⚙️ Step 2: Create Your First Project

Generate a new NestJS project using the CLI:

bashCopyEditnest new my-first-nest-app

You’ll be asked to choose a package manager — choose npm or yarn.

Once the installation is done:

bashCopyEditcd my-first-nest-app
npm run start:dev

Open your browser and visit:
👉 http://localhost:3000

You should see:
Hello World!

🏗️ Step 3: Understand the Project Structure

NestJS uses modules, controllers, and services.

Here’s the core structure you’ll see:

lessCopyEditsrc/
├── app.controller.ts      // Handles incoming HTTP requests
├── app.service.ts         // Contains business logic
├── app.module.ts          // Root module where everything is wired up
main.ts                    // Entry point

🧱 Step 4: Create Your First Module

Let’s say you want to create a simple cats feature.

bashCopyEditnest generate module cats

This creates cats/cats.module.ts.

🎮 Step 5: Create a Controller and Service

bashCopyEditnest generate controller cats
nest generate service cats

This gives you:

arduinoCopyEditsrc/cats/
├── cats.controller.ts
├── cats.service.ts
├── cats.module.ts

Update cats.controller.ts to look like this:

tsCopyEditimport { Controller, Get } from '@nestjs/common';
import { CatsService } from './cats.service';

@Controller('cats')
export class CatsController {
  constructor(private catsService: CatsService) {}

  @Get()
  findAll() {
    return this.catsService.findAll();
  }
}

Update cats.service.ts:

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

@Injectable()
export class CatsService {
  private readonly cats = [{ name: 'Whiskers', age: 2 }];

  findAll() {
    return this.cats;
  }
}

Now go to:
👉 http://localhost:3000/cats
And you'll get a list of cats!

Add a POST Route

In cats.controller.ts:

tsCopyEditimport { Controller, Get, Post, Body } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  constructor(private catsService: CatsService) {}

  @Get()
  findAll() {
    return this.catsService.findAll();
  }

  @Post()
  create(@Body() cat: { name: string; age: number }) {
    return this.catsService.create(cat);
  }
}

In cats.service.ts:

tsCopyEditcreate(cat: { name: string; age: number }) {
  this.cats.push(cat);
  return { message: 'Cat added', cat };
}

Use Postman or curl:

bashCopyEditcurl -X POST http://localhost:3000/cats \
-H "Content-Type: application/json" \
-d '{"name": "Tiger", "age": 3}'

What Next?

  • Explore DTOs and Validation with @nestjs/class-validator

  • Connect a database using @nestjs/typeorm or @nestjs/mongoose

  • Use Guards, Pipes, and Interceptors for advanced use cases

  • Add authentication with Passport and JWT

Resources

Recap

NestJS gives you a full toolbox to write clean, testable, and enterprise-grade APIs with Node.js. You’ve just built your first NestJS app with a custom controller, service, and route!

Keep building from here—NestJS is massive, but beginner-friendly with the right steps.



Getting started

main.ts (engine startup/entry point)

app.module.ts = main module/root module: specifies imports, controllers and providers (services)





Related posts

August 2, 2024

A Young Father's Guide To Writing Your First API

August 2, 2024

A Young Father's Guide To Writing Your First API

August 2, 2024

A Young Father's Guide To Writing Your First API

Got questions?

I’m always excited to collaborate on innovative and interesting projects.

Got questions?

I’m always excited to collaborate on innovative and interesting projects.

Got questions?

I’m always excited to collaborate on innovative and interesting projects.

©2024

©2024

©2024

Get Template for free

Create a free website with Framer, the website builder loved by startups, designers and agencies.