Skip to main content

Back-End Development

Build Scalable and Reliable Node.js Apps with Ease: Introducing Nest JS

Nest JS for beginners

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. We’ll build an application that covers various Nest JS fundamental and advanced topics such as Services, Controllers, Modules, Repositories, 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: the deployed version of my app.

Throughout this series expect clear diagram-based explanations, handy GitHub Gists code snippets and complete access to the app source code that we’re crafting. Let’s take a closer look and really understand the Nest JS!
and last thing comments/suggestions are always welcome πŸ™‚

An Overview of Nest JS:
Nest was created by Kamil Mysliwiec. It has more than 62.6 thousand GitHub stars ⭐(updated on 1 Feb 2024) & still growing.

Nest JS framework GitHub Star

Nest JS is an 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 a lot of tools out of the box like Dependency Injection πŸ”—, Microservices β›“, Web socket βš™, Testing utilities πŸ“ and Supports REST and GraphQL API πŸ”Œ

Today top companies like Adidas, Red Hat, GitLab, Mercedes-Benz, IBM, Capgemini and many others use it in their production.

A Closer Look at Nest JS
Nest JS is a backend framework. When I say backend one thing directly comes into mind is HTTP Request/Response. Let’s understand how it works with the help of the diagram below.

Image1

Fig. Client/Server Architecture

Image4

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 will not vary regardless of any Framework, Library, Language or Platform.Β 

Below is a more detailed breakdown of the above Client/Server architecture diagram:
Image7
Fig. Client/Server Architecture Detailed
  1. πŸ“« Receive a request
  2. Might do some 🏳 Validation on the data that is contained in the request (validate some data in the body of the request)
  3. Make sure the user isΒ  πŸ— Authenticated/Authorize to send us a request
  4. πŸ“‘Β Route or Point the request to a particular function to handle a request that might result to
  5. Running a particular 🎲 Business logic and eventually
  6. Access or 🧰 Store data inside a database.

And then as a result of the 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 in the Nest JS Way

Image3Fig. 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 JSImage18

Fig. Parts of Nest JS

Folder structure and important files

Image19

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 the Angular framework(Angular bootstrapping)Image6

Fig. app.module.ts

Module Elements:

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

Note: Dependency Injection – This is an important concept to understand in Nest JS and other frameworks. Will plan do explain with building app in upcoming blogs. stay tuned…

  • main.ts: Entry point of application
Image11Fig. main.ts file
  • Controller: Each controller has more than one route and each route serves different actions like Create, Update, DeleteImage9
Fig. app.controller.ts

Decorators and its TypesImage15

Fig. Types of Decorators

service.ts
Image19

Image2

Scaffold Our First Nest JS App with the Nest CLI tool

  • Prerequisite: Latest Node JS version
  • Installation: To scaffold the project let’s install Nest CLI and run the below command
> 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 be adding all our business logic.

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

In the above code we added a greeting() method to return a message. In app.controller.ts lets add one custom controller endpoint /hello

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

After adding the lines above you will find an explanation in the diagrams below:

Image10

Image20

Extra Tips: To deploy your project with Vercel follow the step-by-step guide here. (if you like my content & writing style please give me Clap πŸ‘ on my blog on medium.com)
See my deployed demo here: https://nest-j-scaffold.vercel.app/hello
Postman request test:Β  https://nest-j-scaffold.vercel.app/hello

Image16

Thoughts on “Build Scalable and Reliable Node.js Apps with Ease: Introducing Nest JS”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Raj Lanjewar, Associate Technical Consultant

Raj Lanjewar is a UI developer at Perficient Nagpur. He is passionate about learning and exploring new technologies that keep him productive.

More from this Author

Follow Us