What Is Nest.js?
Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Under the hood, Nest makes use of robust HTTP Server frameworks like Express (the default) and optionally can be configured to use Fastify as well!
Nest provides a level of abstraction above these common Node.js frameworks (Express/Fastify), but also exposes their APIs directly to the developer. This gives developers the freedom to use the myriad of third-party modules which are available for the underlying platform.
If a NestJS developer has sufficient knowledge about JavaScript, he will be able to build the most effective application in this framework. The primary objective of Nest JS is to solve the architectural issues of Node JS by offering back-end apps a modular structure to organize code into separate modules. The notable thing to work with Nest JS is that it has been fully built with TypeScript that is quite beneficial when it comes to developing an app.
What can Nest do? Amaze me!
- Interact with your database easily in an abstracted manner. You just create and annotate classes with decorators. This is called Object Relational Mapping (“ORM” might sound more familiar)
- Create service layer classes easily to handle the business logic of your application. Then, inject (read: “use”) these services in other services with the help of NestJS’ dependency injection
- Create so called “Controllers”, which can easily respond to HTTP requests. This is your main way to interact with the clients of your Nest app (React? Vue? Mobile app? Another service? Anything.)
- Secure the endpoints you create with Controllers. Nest makes it easy to guard the API of your app, so only (for example) logged in users can access them (be it basic auth., JWT etc.)
- Create microservices with Nest. Cool.
- Build a GraphQL interface easily
- Integrate message queues into your app (maybe you need to do some asynchronous data processing?)
- …and much more
Why Nest JS?
Let me first begin by listing the fundamentals of any backend system, and my review will be based on weather Nest JS covers them or not.
Back-end fundamental areas:
- Server architecture
- Database administration
- Data transportation
- Security
- Integrations and third-party libraries (tools) support.
Next, I will depend on the following criteria to measure the pros and cons
Architecture
- Nest JS is a highly opinionated framework, the architecture paths are already built, and the developers follow the provided structure guidelines (The architecture is heavily inspired by Angular's solid structure).
- In my opinion this makes it easier for the team to understand each others code and find things quickly.
Performance
- There are two important aspects of software performance: responsiveness and scalability,
- Nest JS is perfect to handle these aspects.
- Nest JS offers compatibility with other libraries, such as Fastify or Express.js out of the box.
Documentation
Their official documentation covers all the backend aspects I've mentioned earlier, using the latest trend technologies and best practices.
Testing
You can use any testing framework that you like, as Nest JS doesn't force any specific tooling, but it provides integration with Jest and Supertest out-of-the-box.
Integrations, Libraries, Security
- All Node JS packages and integrations are available.
- Many aspects are ready out-of-the-box, such as: Database (TypeORM), Data Validation, Caching, Task Scheduling, Logging, Cookies, File Upload, Server-Sent Events, Authentication, Authorization, Encryption, Hashing, Helmet, CORS, CSRF, Limiters, GraphQL, WebSockets, Microservices, Redis, MQTT, Kafka and more
Summary
- NestJS adopted both TypeScript and Angular features.
- NestJS uses TypeORM which gives leverage to connect with SQL and NoSQL databases.
- Since NestJs is an Abstraction over the NodeJS. So we can use NodeJS libraries
- NestJS surrounds your route handler body with try catch blocks, and it binds error-handling middleware.
If you are a Node JS and TypeScript fan then definitely you should keep Nest JS in your mind for your next project.
And if you are familiar with .NET Core structure and core concepts, you will definitely fall in love with Nest JS.