The JavaScript Enterprise Edition (JsEE)?
In a few minutes, you won’t be able to do without NestJs. Launched in 2017, version 7 of this open source framework for NodeJs, has nearly 45,000 stars on Github.
With its scalable aspect and its architecture strongly inspired by Angular (Google’s front-end framework), it was quickly adopted by the community to allow developers to move easily from the front-end to the back-end. Why has NestJS, among many other frameworks, become so popular?
Have you ever wanted to work on an easily scalable application in NodeJs with all the advantages of an enterprise framework like Spring or .NET, while taking a long-term view of your project? Or allow your application to be standardised between your development teams in a single language? This is what NestJs allows.
NodeJs
Back-end in Javascript?
NodeJs allows server-side JavaScript execution with very little code. This is useful for creating high-performance server applications (such as small projects or back-end side projects). It is its simplicity and minimalism that have made it so successful.
It has the major advantage of running on a V8 JavaScript engine , which makes it very powerful. Another advantage is that it can run in a single process without creating a new thread for each request.
The limitations of NodeJS
Given the minimalism of the code, it may be necessary to employ significant resources for simple uses. Moreover, each developer structures his/her code (middleware, routing, error handler…) in his/her own way, which can make it difficult to read. For example, if two teams are working on the development of a NodeJs application, it is unlikely that the standardisation of their code will follow the same pattern. So, even with these frameworks, maintainability becomes difficult as the project and teams grow.
The main Frameworks for NodeJs
A very large number of frameworks for NodeJs have been created, each with their own specifics depending on how they are used.
Some focus solely on the provision and creation of REST services (restify, loopBack). The objective here is to provide an easy-to-use framework for exposing services to other web apps.
Others aim to be more complete (such as Sails), in an attempt to provide numerous functionalities, even allowing front-end and back-end management of applications (with an MVC model).
The most popular framework is probably Express.js, a fast, non-restrictive and minimalist framework. However, simplicity has its limits… And now NestJs comes into play.
The NestJs framework for NodeJS
Language and paradigm
Should we really be talking about NestJs? It should be NestTS (TypeScript). Indeed, it is one of the only frameworks entirely built around TypeScript (inspired by Angular2). This not only provides a strongly typed code, but also a framework exploiting all the advantages of TypeScript with decorators, Types, Tuples, etc… Moreover, for the purists, it is fully vanilla JS compatible (despite some missing features).
And that’s not all: in terms of programming paradigms, it relies heavily on OOP to manage your modules and object resources. But also on functional and reactive programming for the management of server requests.
Servers and Performance
NestJs has such a high level of abstraction that it can be configured on multiple HTTP servers (like JEE where you can choose TomCat, WildFly or other), based by default on ExpressJs.
If your application needs to respond to a large number of requests per second, you can interface with Fastify. This represents a good alternative in terms of performance:
What tools for NestJs?
Another advantage of the NestJs framework is that you no longer have to search GitHub for new libraries. The framework provides you with the right tool for your specific use case.
« Build once, use everywhere »
There are many reusable concepts to manage your server such as interceptors for error handling, pipes to check the data of incoming requests, guards for security.
The framework also integrates the most popular JavaScript back-end libraries, such as PassportJs, Helmet, JOI, Cron, TypeORM, Mongoose, bull…
All you have to do is integrate them into your project with, for example, a package: @Nest/joi. All information about each library is presented in the framework documentation, with clear use cases.
And in addition to all these classic REST API tools, NestJs goes one step further by aiming to be “platform agnostic“. That is to say, you will be able to set up a server that is both REST compatible and use this same application to work at the same time with GraphQl (while integrating your favourite microservices: kafka, gRPC, redis…).
The benefits of NestJs
NestJS is strongly inspired by Angular 2, which means that it should be considered as restrictive and with a strong extensibility of its architecture.
A non-restrictive framework allows the developer to design the code as they wish (e.g.: ExpressJs). In contrast, NestJs requires that all projects be structured and standardised in the same way.
This will allow developers to have the same conformity in the way they code on the back-end (and possibly have a team coding applications from end-to-end in TypeScript).
It is structured in such a way as to build the application module by module (Domain-Driven Design). Your entire application is built around this notion of module (also called domain or resource).
If we take the example of REST application logic, we can instantly generate a new resource with all its CRUD controllers via the NEST CLI (Command Line Interface) commands.
DEMO – Let’s have a try
The disadvantages of NestJs
- If you’ve never used Angular (or TS), it takes a little time to get up to speed to understand the module structure of a NestJs application.
- If you want to be exhaustive in the declaration of controllers, coding numerous annotations around each one can quickly make them verbose.
- When you need to go into detail about some of the annotation options, for example, you’ll need to dig into the @Nest/core source code. This could be improved by making them appear in the NestJs API doc.
- NestJs remains a tool subject to some of the limitations of NodeJs. The heavy calculations of your application will probably have to be executed on another type of server.
To conclude
NestJS is a full-featured back-end framework that presents itself as the best alternative for creating a medium to large application in NodeJS. Unlike the others, it has the intelligence to draw on and integrate many concepts (or libraries) that have already proven themselves. Thus, its Angular-inspired architecture allows you to focus on your real added value while ensuring the quality of the code produced. Its main assets? It allows for scalable, testable and maintainable applications. But it also requires some practice to get the hang of it.
Still not convinced? We advise you to have a look at the documentation (very well done) which is full of good practices and integrates useful libraries, whatever the languages used. We also recommend that you return regularly as NestJs’ growing popularity ensures it has the support of a very active community.