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 use cases
- Single-page apps: NestJS provides the serve-static package, which allows you build single page applications as fast as possible
- Reusable apps: Since NestJS supports and implements object-oriented programing, it is easier for you to build reusable web applications
- Middleware applications: The
@nestjs/commonpackage allows you to implement middleware in your web applications
- RESTful APIs: NestJS is heavily based on Angular and therefore uses Angular-like modules, services, controllers, pipes, and decorators, making it ideal for developing scalable, testable, loosely coupled, and easily maintainable RESTFul APIs
- Serve and access static files using a browser: The serve-static package supports templating engines like Handlebars and EJS, which allow you to easily serve HTML templates
- Enterprise web applications: NestJS applications scale, making it an ideal choice for building large, enterprise-level applications
- Ecommerce web applications: Easily combine NestJS with frontend frameworks like React, Angular, and Vue to build ecommerce websites
Who uses NestJS?
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.
What is Hapi?
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.
Hapi use cases
- Network applications: Hapi is the ideal choice for building a network applications as fast as possible without spending time building infrastructure
- Server component for single-page applications: Use Hapi with frontend frameworks like Backbone, React, and Angular to create impressive single-page applications
- HTTP-based software applications: Hapi has an h2o2 plugin that provides a handler type for proxying HTTP requests to other endpoints
- e-commerce web applications: Hapi can be combined with frontend frameworks like React, Angular, or Vue to build e-commerce websites
- JSON APIs for web applications: Hapi is a classic choice for building JSON APIs, allowing for easy implementation of routing, input and output validation, authentication, and caching
- Static or data-driven websites: Hapi integrates with several HTML-templating languages, including Handlebars and Jade, making it simple to generate HTML documents from dynamic data. Hapi also makes it simple to deploy apps that use cookie-based authentication and sessions to keep the state of the app consistent between page requests
Who uses Hapi?
At the time of writing, about 78 companies reportedly use Hapi in their tech stacks, including TechStack, Commercetools, Brainhub, Brainhub, Beam, and Platform.
NestJS vs. Hapi
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.
More great articles from LogRocket:
- Don't miss a moment with The Replay, a curated newsletter from LogRocket
- Use React's useEffect to optimize your application's performance
- Switch between multiple versions of Node
- Learn how to animate your React app with AnimXYZ
- Explore Tauri, a new framework for building binaries
- Compare NestJS vs. Express.js
- Discover popular ORMs used in the TypeScript landscape
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.
Create a project
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
Which framework should I choose?
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!
LogRocket: Full visibility into your web and mobile apps
LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.