DEV Community

Cover image for NestJS vs. Hapi
Matt Angelosanto for LogRocket

Posted on • Originally published at blog.logrocket.com

NestJS vs. Hapi

Written by Ekekenta Odionyenfe✏️

Both NestJS and Hapi are Node.js frameworks, which is why many developers question which to use in their project. The two frameworks have a lot of noticeable differences, one of the most significant being NestJS’ built-in 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 overview

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 of 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/common package 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 242 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 76 companies reportedly use Hapi in their tech stacks, including TechStack, Commercetools, Brainhub, Brainhub, Beam, Platform, etc.

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

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, Nest 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 slower than nest-fastify.

Popularity

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 46,000 stars on GitHub, 1,100,000 weekly downloads on npm, and 59 package dependents.

On the other hand, Hapi has over 13,000 GitHub stars, 500,000 weekly downloads on npm, and 547 package dependents.

Application architecture

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

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 application. When a Hapi application is scaled, the Hapi server breaks down the business logic into microservices, which are easy for the server to manage.

Database support

NestJS is database agnostic, and it allows you to easily integrate with any SQL database, including MySQL, Oracle, SQL Server, or 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.

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.

NestJS project

To create a NestJS server, you need to install the NestJS CLI with the command below:

npm install @nest/cli
Enter fullscreen mode Exit fullscreen mode

Then, create a NestJS application with the command below:

nest new project-name
Enter fullscreen mode Exit fullscreen mode

The command above 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
Enter fullscreen mode Exit fullscreen mode

Hapi project

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
Enter fullscreen mode Exit fullscreen mode

Then, run the command below to create a new project:

hapi my_app
Enter fullscreen mode Exit fullscreen mode

Now, change the directory into the project folder and install the required dependencies:

cd my_app && npm install
Enter fullscreen mode Exit fullscreen mode

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.

Conclusion

In this article, we compared two major Node.js frameworks, Hapi and NestJS, based on their use cases, performance, popularity, architecture, scalability, 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 apps

LogRocket signup

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.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.

Try it for free.

Top comments (0)