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 is a framework for creating scalable, efficient, server-side applications with Node.js. It uses progressive JavaScript with TypeScript support and incorporates features of object-oriented programming, functional programming, and functional reactive programming.
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 applicationsAt 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.
On the other hand, Hapi has over 13,900 GitHub stars, 563,000 weekly downloads on npm, and 547 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 (@Get
, @Post
, @Put
, @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:
app.controller.spec.ts
app.controller.ts
app.module.ts
app.service.ts
main.ts
Finally, run the server with the command below:
npm start:dev
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:
hapi my_app
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
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowJavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.
Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.
Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.
Build a real-time image background remover in Vue using Transformers.js and WebGPU for client-side processing with privacy and efficiency.
One Reply to "NestJS vs. Hapi"
Great information! Thanks for that. Just one comment, in the performance section you wrote: “Express handles 38,510 requests per second, which is slower than nest-fastify.” but as I understand Express can handle more requests than the other two. If that is true, why is Express slower?