Why NestJS?

A framework for SHv3. Similarities, Popularity, Architecture, Feature, Support, and Conclusion.

Piyush Patel
LifeatIkigai
5 min readJul 25, 2020

--

Photo by Jon Tyson on Unsplash

What is NestJs

NestJS is one of the fastest trending NodeJS frameworks. NestJS provides more scalability for the project that uses Typescript and uses Express under the hood. So it makes the app compatible with express middlewares.

Image Source: Getting started with NestJS & TypeORM (+bonus NestJS Admin)

It also enables us to write highly testable, scalable, loosely coupled, and easily maintainable applications. To do so, it uses express under the hood. It acts as another layer on top and makes a lot of things easier for us.

Similarities

Angular deeply inspires the NestJS framework. For Angular developers, getting familiar with the core concepts of NestJs is easy.

Not only Angular, but it also borrows concepts from a lot of other frameworks as well, namely:

  • It borrowed the idea of modules from Angular
  • Repositories, Services, and Entity from Spring and many more.

NestJS Popularity

NestJs is increasing in popularity quickly. Even so, it is currently in the early adoption stage and thus it is being molded according to the current challenges faced by the industry keeping in mind the best practices as well.

Image Source: How to Test Your NestJS Application with Jest in 4 Steps

It has one of the best support of any framework in the industry as can be seen from the figure.

Architecture

NestJS provides a controlled architecture, heavily inspired by angular. It’s an opinionated framework, providing reusable components and is highly scalable.

It also forces you to write your code and structure in a structured manner, this enforces a code standard that is invaluable in a team setting. It also enforces a folder structure that causes ease in maintenance.

Microservice support

It also provides ample support for MVC and SOA in addition to Microservices.

It is an annotation-driven framework. A lot of javascript code is hidden behind the annotations, resulting in a very clean, easily understandable code.

It also provides support for a lot of such message brokers built-in, in fact it is built in such a way so as to compliment them.

Features

In the NodeJS world, it is very common to attach some properties to the request object, and manually extract them in the route handler.

However, in NestJS via the use of decorators, we can make the code more readable and transparent.

Here, the User decorator will extract the user object from the request and provide it to the route handler as a parameter.

Another thing that is highly used in the server-side application is request validation, that is, in a typical NodeJs application, a logic for request validation resides in the route handler. NestJs provides Pipes which is used to validate and transform the request data.

Here, we are separating the logic for request validation away from the route handler. We shipped a validation task to a pipe and wrote the logic there. When the handler is called, the body parameter in the handler will already be validated by the pipe. If there is any validation error, an exception can be thrown from the pipe, and the request won’t even reach the route handler.

Similarly, authentication that, in a typical NodeJS application, resides in the middleware can be done using Guards that can be deployed using decorators.

As is evident from all these examples, decorators play a pivotal role in Nestjs, you can do a lot using them and it is often the recommended way too. They are for separating some of the reusable logic away from a route handler, adding metadata to the route.

Adherence to SOLID principles

It also adheres to the SOLID principles that make software designs more understandable, easier to maintain, and easier to extend.

S — Single Responsibility Principle: Nest provides a notion of Modules to confirm Single Responsibility.

O — Open Close Principle: Nest provides TS driven environment with interfaces and inheritance supported by design

L — Liskov Substitution: is the ability to use subclass object at places of superclass maintaining the method behavior, possible with TS and right use of inheritance

I — Interface Segregation: writing relevant and lean interfaces depicting accurate behaviors

D- Dependency Injection: supported by design in Nest

I like Nest because of its conformance to some of the best programming models by design, which is a key to success in the long run. — Yashesh (Tech Lead) from Team Ikigai

Conclusion

In essence, NestJS is an excellent framework, and the team loves it. Especially in these times where more and more companies are adopting the microservices format. It is a framework that is built keeping microservices in mind.

The backend written in NestJS is more scalable and easier to collaborate on as well. Everything just works as well due to the many packages provided by the awesome NestJs community.

Comment your views. Appreciate by claps if you find it helpful.

Thanks for your valuable inputs! Pramod, Mehul, Vishal, Yashesh, Avinash, Rahul, Harsh, and Tarun.

Your inputs have made this story more insightful.

--

--

Piyush Patel
LifeatIkigai

blend of tech, entrepreneurship and startup. Cofounder @SalesHandy