If you’re interested in learning a robust, efficient, and scalable enterprise-level server-side framework, you’ve landed on the right blog !   We’ll start with a quick introduction, and in this post and the ones to follow, we’ll dive straight into the practical part. Together, we’ll build an application that covers various Nest JS fundamental and advanced topics, such as Services, Controllers, Modules, Repository, Pipes, Dependency Injection, and database connectivity using TypeORM. To make things more interesting, we’ll deploy this application using top-notch tools (hint: Vercel or  StackBlitz) and put it to the test with the powerful Postman tool.

What will we build? take a sneak peak:  deployed version of my app

Throughout this series expect clear diagram-based explanations, handy GitHub Gists code snippets, and complete access to the source code of the app we’re crafting. Let’s take a closer look and really understand the Nest JS!

Starting with quick overview: 
Nest was created by Kamil Mysliwiec. It has more than 62.5 thousand GitHub stars (taken on 30 Jan 2024) still growing.

Nest JS is a Angular inspired server-side Node JS backend framework, Built on top of Typescript,  Open-source platform, When we say Platform that means it comes with lot of tools out of the box like Dependency Injection, Micro services, Web socket, Testing utilities and Supports REST and GraphQL API out of the box.

Today it is used by top companies like Adidas, Red Hat, GitLab, Mercedes-Benz, IBM, Capgemini etc. in their production.

 

Let’s take a closer overlook of NestJS:
Nest JS is a backend framework. When I say backend one thing directly comes into mind is HTTP Request/Response. Lets understand how it works with the help of the diagram below.

Fig. Client/Server Architecture

Fig. HTTP request

 

How HTTP request works 
Every HTTP server you ever going to create may have a Request/Response cycle.

Users are going to request to your server, inside the server you will have some amount of code that will process that request. You might validate data inside the request and eventually you might formulate the response and send it back to whoever made that request.

Whenever you build the server,  the request and response cycle will be almost the same and it’s not vary regardless of any Framework, Library, Language or Platform. 

Below is more detailed breakdown of above Client/Server architecture diagram:
Fig. Client/Server Architecture detailed

Receive a request
Might do some Validation on the data that contains in the request (validate some data in the body of request)
Make sure the user is Authenticated/Authorize to send us a request
Route or Point the request to particular function to handle a request that might result to
Running a particular Business logic and eventually
Access or Store data inside a database.

Then as a result of the above entire process we are going to formulate a response and send it back to whoever made that request.

Maybe in some cases we might not do authentication or exclude some of the steps but more or less the process is going to be the same.

 

Let’s understand above explanation in NestJS way:

Fig. Nest JS Client/Server Architecture

In Nest JS we have special tools to address each step.

Pipes: Helps to validate data of incoming requests.
Guard: Make sure the user who is making requests to the application is authenticated/authorized.
Controllers: Routing logic.
Service: Handle data access and business logic.
Repository: Handles data stores in db.

 

Building Blocks of Nest JS:

Fig. Parts of Nest JS

Quick overview to folder structure and some important files:

Fig. Nest JS folder structure

Modules: As the name suggests it is used to organize code split features into logical, reusable units, every Nest application has at least one  root module, It’s used to bootstrap our application same as Angular framework(click here to read about Angular bootstraping)

Fig. app.module.ts

Module Elements:

Controllers: Entry point of request, handling incoming request of application and responding answer to the clients
Imports: List of modules which come under the modules.
Providers: Instruction for Dependency injection system on how to obtain a value for dependency
Exports: Public API for module(explicitly export – main diff between Angular and NestJS)

Note: Dependency Injection – Very important concept to understand in Nest JS and other current frameworks, I am planning to explain this concept in upcoming blogs… stay tuned.

 

main.ts: Entry point of application

Fig. Parts of Nest JS

Controller: Each controller has more than one route and each route serve different actions like Create Update, Delete

Fig. app.controller.ts

 

Decorators and its types:

Fig. Types of Decorators

service.ts:

 

Now lets scaffold our first Nest JS app with official Nest CLI tool:

Prerequisite: Latest Node JS version

Installation: To scaffold the project lets install Nest CLI

> npm i -g @nestjs/cli
> nest new project-name

Let’s add some custom code in app.service.ts file here we already have getHello()function that returns Hello World! String as discussed earlier in this file we will add all our logics here.

Note: copy code changes from here

greeting(): string {
return ‘Welcome to NestJS ‘;
}

In above code we added greeting method that is returning string message In app.controller.ts lets add one custom controller endpoint /hello

@Get(‘/hello’)
  greeting(): string {
  return this.appService.greeting();
}

Extra Tips: To deploy your project with any tool like Vercel, Heroku, I already have a detailed step by step guide for deploying your Nest JS app with Vercel click here to follow.
See my deployed demo here(Deployed on Vercel): https://nest-j-scaffold.vercel.app/hello
Postman request testhttps://nest-j-scaffold.vercel.app/hello