Lang
Blog

Why Choose Nest.js over Other Node Frameworks?

A structured way of building server-side applications.

ByPayal Mittal
July 16th . 5 min read
Why Choose Nest.js over Other Node Frameworks?

With so many outstanding frameworks available out there like Next.js, Express.js, Koa, Hapi, or Fastify, why would you go for something new and different? Or the better question- why would you choose Nest.js, over these widely-used, well-known, and ‘usual’ frameworks, you’re quite used to.

Well, I know just the right answer to this question.

Perhaps, you want the development process to complete quickly and efficiently by using the boilerplates and controllers. Maybe you want to build scalable and easy-to-maintain apps. Or, you just want to choose a convenient and effective framework to work with.

In either way, it turns out that Nest.js might fulfill all these needs.

In this blog, we’ll get to know the beginner’s basics about NestJS and its properties that separate it from the pool of other Node.js frameworks-

What is Nest.js?

“A progressive Node.js framework for building efficient, reliable, and scalable server-side applications.”

NestJS is an open-source Node.js backend framework, based on Express.js, that takes advantage of both TypeScript and JavaScript.

It was first designed and launched by Kamil Myśliwiec for the development of server-side applications. At present, NestJS has around 23k stars on GitHub and the average rate of weekly npm download is about 180k.

NestJS is compatible with progressive JavaScript and built with TypeScript and combines elements of these three concepts-

  • OOP (Object Oriented Programming)
  • FP (Functional Programming)
  • FRP (Functional Reactive Programming).

Some magnificent properties of Nest are-

  • Extensibility: Flexibility to use other libraries
  • Versatility: Adaptive ecosystem that is fully-fledged for all types of server-side applications.
  • Progressiveness: Brings JavaScript features and design patterns into the Node community.

Nest.js is very similar to Angular.js in many ways, it will not seem strange for those who’ve worked on Angular before. Like angular, NestJS comes with its own dependency injection container, a utility that seems forgotten in other frameworks. The architecture and syntax also match with that of Angular.

Building Blocks of Nest.js

Given below are the building blocks of Nest applications, there are more of them but these 3 are the most important ones. Let’s get a quick look at them-

Controllers

“Controllers are responsible for handling incoming requests and returning responses to the client.”

It receives specific HTTP requests that come into your application and the routing mechanism controls which controller will receive which request. It, then, formulates a proper response to go out.

  • A basic collector is created with the use of classes and decorators.
  • Each controller has different routes to perform different operations.

why-choose-nest-js_1.jpg

Providers

Providers are but a fundamental concept. It can be a repository, service, or helper.

  • A provider is a plain JavaScript class with an @Injectable() decorator that can inject dependencies.
  • These injections have a lifetime (scope), i.e. when the application shuts down, so does its providers.

The purpose behind designing these providers was to “abstract all forms of complexity and logic to a separate class”.

why-choose-nest-js_2.jpg

Modules

A module is a class with a @Module() decorator that organizes the application structure components.

Each application has a root module that is used to build the application graph that further resolves the dependencies and relationships between provider and module.

This decorator sends metadata to Nest.js required to determine which components, controllers, or other resources will be used in the application code and group them together into a single set. Each module encapsulates a closely related set of capabilities.

why-choose-nest-js_3.jpg

Why Nest.js?

There are many reasons that make Nest.js preferable over other NodeJS frameworks, some of them are -

  • Compatibility with TypeScript
  • Support Monorepo
  • Made for monoliths and microservices
  • DevOps support ready
  • Powerful CLI
  • Built-in support for microservices & transport layers
  • Dependency injection container
  • Easy database interaction
  • Building GraphQL interfaces easily
  • Framework adaptive nature
  • Domain-driven development
  • Angular based folder structure
  • Built-in Exception Filter
  • Use of third-party modules
  • Built-in inversion of control

We’ll understand some of these features in more than one line, you should have a look here -

Architecture & CLI that You May Fall in Love With

Through years of updates and upgrades, JavaScript has now become the most popular language for developing frontend and backend web applications in the world of programming (Thanks to Node.js). And is the mainstay of the creation of some amazing frameworks such as Angular and React.

However, it doesn’t help to have a large stack of libraries, superb tools, and helpers, the main problem remains intact, that is- Architecture flexibility.

But, Nest.js with its out-of-box microservice architecture (similar to Angular.js), makes developer’s life a lot easier and happier by enabling them to build scalable, loosely coupled, highly testable, and easily maintainable applications.

If you are having second thoughts about whether or not Nest is for you or not, you should check out the Nest CLI- A Command Line Interface (CLI) is the tool that assists in multiple ways- from helping in initialization and development to maintenance of the Nest applications.

Not only the Nest CLI assist in project scaffolding and building applications for production but also embodies best-practice architectural patterns. Also, it supports monorepo.

You can install the Nest CLI and set up a new project using the following command -

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

Framework Independence

Under the hood, Nest uses powerful HTTP server frameworks like Express and is also compatible with other frameworks such as Fastify.

Nest makes use of a framework adapter to achieve this framework independence that works as a proxy middleware to carry out some library-specific implementations.

Though Nest uses Express to solve some design issues, Fastify can do the same thing with better results and faster speed. In order to migrate to Fastify from Nest, you just have to choose the built-inFastifyAdapter.

First, you need to install this package -

$ npm i --save @nestjs/platform-fastify

Now, you can use the FastifyAdapter, as follows -

import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';

Built-in Exception Filters

One of the great features of Nest.js is- it contains a built-in exception layer that handles all those exceptions that were not caught by the application code and sends an appropriate response to the request.

In case of an unrecognized exception (neither HttpException nor its inherited class), this built-in exception layer generates the following response to the request -

{
"statusCode": 500,
"message": "Internal server error"
}

Note: A standard (and most common) set of exceptions are defined by Nest.js, inherited from the base HttpException, any other exception will be considered ‘unrecognized’.

why-choose-nest-js_4.jpg

Get Started with NestJS

In order to get started with Nest.js, you need to make sure that you’ve got the supported version of Node. Hit node -v to check which version of Node is installed in your system, if it’s not equal to or higher than v10.13.0, update the existing version.

To set up the project -

#With Git (Install the TypeScript starter project)-
$ git clone https://github.com/nestjs/typescript-starter.git project
#Install the JavaScript starter project-
$ git clone https://github.com/nestjs/ javascript-starter.git project
$ cd project
#install dependencies and start the server-
$ npm install
$ npm run start
#Manually create the project with npm (or yarn)-
$ npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata

To get more practical information, click here.

I hope this blog will be a help for you. If you found it insightful, you must give Nest.js a fair chance and make a go for it.

Feel free to share your views with us, we’ll be glad to hear from you.

Thanks for reading!!

Share:
0
+0