Both NestJS and Hapi are Node.js frameworks, which is why many developers question which to use in their projects. The two frameworks have a lot of noticeable differences, one of the most significant being NestJS’ inbuilt support for TypeScript.
In addition, NestJS combines elements of object-oriented programming, functional programming, and functional reactive programming. Hapi and NestJS have several other key differences, which we’ll discuss throughout this tutorial. Let’s get started!
NestJS uses strong HTTP server frameworks like Express and Fastify that provide a degree of abstraction over major Node.js frameworks. But, they also expose their APIs to the developer directly, allowing developers to use the plethora of third-party modules available for the underlying platform.
@nestjs/common package allows you to implement middleware in your web applications
At the time of writing, over 285 companies reportedly use NestJS in their tech stacks, including Postclick, Quero, Kevin, Ubuddy, Our Stack, Frontend, and more.
Hapi, or HTTP API, is an open source framework for developing scalable web applications. One of the most basic use cases of Hapi is to build REST APIs. You can build API servers, websites, and HTTP proxy applications with Hapi, and it allows developers to spend less time establishing infrastructure and more time writing reusable application logic.
At the time of writing, about 78 companies reportedly use Hapi in their tech stacks, including TechStack, Commercetools, Brainhub, Brainhub, Beam, and Platform.
Now, let’s compare the features of NestJS and Hapi to better get a grasp of their differences, weaknesses, and strengths.
Performance is one of the key features of any good framework, and NestJS is a big hit among developers when it comes to performance. Since NestJS integrates with Node.js and modules like Express and Fastify, it can quadruple developers’ productivity and application performance while saving time.
The NestJS CodeChecks benchmark shows that a nestjs-fastify, NestJS and FastifyAdapter, “Hello, World!” application handles about 30,001 requests per second. On the other hand, the Hapi “Hello, World!” application handles 29,998 requests per second, and Express handles 38,510 requests per second, which is faster than nest-fastify.
Both NestJS and Hapi are among the most popular Node.js frameworks, but one must be more popular than the other. At the time of writing, NestJS has over 48,500 stars on GitHub, over 1,400,000 weekly downloads on npm, and 59 package dependents.
Application architecture describes the patterns and techniques used to design and build an application.
NestJS provides a ready-to-use application architecture known as the Controllers-Providers-Modules that enables developers and teams to build applications that are easy to test and maintain.
On the other hand, Hapi supports the MVC, Model View Controller, architecture, which is configuration-based. This type of architectural pattern aids in the scalability of web applications.
Hapi provides a controller to manage the connection between the data model, or the business logic, and the view component, or the representation layer.
Scalability is the property of a system to handle a growing amount of work by adding resources to the system. NestJS uses scalable HTTP frameworks like Express, which derive from the non-blocking features of Node.js. This, coupled with its out-of-the-box application architecture, allows developers to create highly scalable applications.
Hapi was developed with scalability in mind because it was developed for enterprise applications. When a Hapi application is scaled, the Hapi server breaks down the business logic into microservices, which are easy for the server to manage.
NestJS is database agnostic, and it allows you to easily integrate with any SQL database, including MySQL, Oracle, SQL Server, Postgres, or any NoSQL database, like MongoDB, DynamoDB, etc., simply by installing the database driver in your application.
Hapi also supports different database management systems like MariaDB, MongoDB, SQLite, PostgreSQL, and NoSQL. It enables database connectivity through APIs and data caching to reduce the payload.
NestJS is built to support nested routing by default. You can easily group a set of related routes by concatenating the prefix declared for the controller in the
@Controller decorator. Then, you can use the NestJS method decorators (
@Delete, etc.) to define the path information for each endpoint.
Hapi does not support nested routing by default. However, you can tweak your project a bit to group a set of related routes and reduce repetitive code.
Let’s look at a simple “Hello, World!” server in both frameworks. Both NestJS and Hapi have a similar method of creating applications using the command line interface.
To create a NestJS server, you need to install the NestJS CLI with the command below:
npm install @nest/cli
Then, create a NestJS application with the command below:
nest new project-name
The above code will generate a
project-name directory and node modules. It will install a few other boilerplate files and create and populate an
src/ directory with several core files, shown below:
Finally, run the server with the command below:
Creating a Hapi application from the command line interface is similar to creating a NestJS project. First, you need to get the Hapi CLI with the command below:
npm install -g hapi-cli
Then, run the command below to create a new project:
Now, change the directory into the project folder and install the required dependencies:
cd my_app && npm install
Every framework has its strengths and weaknesses. It all depends on what you want and the type of application you intend to build. Even though NestJS has a lot of advantages over Hapi, that doesn’t mean Hapi isn’t ideal for certain use cases. I won’t advise you to choose Hapi over NestJS or vice versa. If you have an application that requires you to use Hapi, go for it. The same applies to NestJS.
In this article, we compared two major Node.js frameworks, Hapi and NestJS, based on their use cases, performance, popularity, architecture, scalability, support of nested routing, and database support. We also learned how to create a basic project with these frameworks. Feel free to choose the framework that best suits your needs. I hope you enjoyed this article! Happy coding!
Install LogRocket via npm or script tag.
LogRocket.init() must be called client-side, not
StyleX is a build-time, type-safe CSS-in-JS library recently open sourced by Meta. Explore StyleX and the evolution of styling libraries.
Learn to set up a completely custom Astro ecommerce implementation that’s also highly performant and type-safe in this straightforward guide.
Let’s build a Next.js app that implements vector search using Supabase and OpenAI to offer better search experiences for users.