DEV Community

gracesekwao
gracesekwao

Posted on

Hapi Vs Express JS

NodeJS

As a developer who uses JavaScript regularly then you might be familiar with NodeJS which is described on its website https://nodejs.org/en/ as a runtime that runs on Google Chrome’s V8 JavaScript engine. Basically, what this means is that it is used to build server-side and network applications using JavaScript. This can come in handy since developers can use the same language for both the frontend and the backend to build different web applications. Moreover, NodeJS is also very popular cause its highly scalable, fast, has high performance and more importantly, its open source.

These are some of the reasons that make NodeJS very attractive to developers and also many tech companies who adapt this language as means to develop different types of applications.

Some of the use cases for NodeJS are:
• Single Page Applications
• APIs (JSON based)
• Data Streaming Applications
• Data-Intensive Real Time Applications
• I/O Bound Applications

However, it is not advised to use NodeJS for CPU-intensive applications.

Web Frameworks

When building an application, many developers choose to add in a web framework to help them in the process. Web frameworks exist so that they can make developers’ lives easier by reducing some of the burden used in setting up the application. Thus, the main focus for the developer would be the features of the application and less on the configuration.

Moreover, when building applications, there are some cases when there are some duplicated features such as when a user needs authentication, connecting to the database and getting a page etc. It can be time consuming to build all of these functionalities multiple times thus this where web frameworks come in. Without web frameworks, the code would not have a standardized structure and would look bloated due to all the duplications. NodeJS applications are no different, they can be quite cumbersome to set up hence the need for web frameworks.

There are many NodeJS web frameworks but just to mention a few examples there is ExpressJS, Hapi, Koa, Fastify, Restify etc . This article will mainly focus on the two of the web frameworks Hapi & Express and compare between the two of them.

Choosing a framework
When choosing a framework my first step is usually to the https://npmcompare.com/ website which provides a way to compare different NPM modules (in this case our web frameworks). I look into the wiki and see if the module has support, that means if it I have an issue there is a support forum just in case, I have an issue there might be ways to find solutions.

Another issue I take into consideration is when the module was last modified, if the date is was too long ago (for example over a year ago) that means the developers of the module do not update it often thus it is technologically behind and the module might be stale. Staying on the subject of updating a module, I also look into the GitHub link for the module and check if they are making breaking changes often that means that the module is not stable thus, I would not be attracted to that module in the first place. Breaking changes means those changes that would break the client when the module is updated.

There is also a lot of issues to consider when choosing a framework such as the number open issues of the module and the dependencies that they have but all in all it also has to go with the requirements of the application that is being built. For example, when a high fast performance application is required then Fastify might be the way to go.

ExpressJs Vs Hapi: A comparison
It goes without saying that Express is the most popular web framework for NodeJS with 55,134,440 monthly downloads as of the day this article was written.

If you’re a developer then you are probably familiar with HTTP methods which are a way of enabling communication between clients and servers and some of these methods are GET, POST, PUT, DELETE, PATCH, HEAD and OPTIONS. Express uses middleware to respond to HTTP requests coming from the clients. Just like the name indicates, middlewares are something you put between software layers. In express, middlewares are functions that have access to the HTTP request sent by the clients, the response and the next middleware function to execute. This means that each of the following middlewares in the stack access to these objects until the function is complete thus creating some kind of a chain. The following figure 1 below shows the architecture they all connected.

Alt Text
Figure 1: ExpressJS architecture

On the other hand, Hapi (short for HTTP Api) uses plugins to do a similar job as ExpressJS. These plugins can either created or you can use the existing ones that are available in the module. These can be used to handle things like authentication, logging, cookie parsing and many more where in contrast in Express you would have to use middleware to do the same thing.

In ExpressJS to get data in a sent in from a request (for example data sent in a form) from the client you would need to parse it first to access it. And this is done by the middleware function called body-parser. However, in Hapi this is done by its internal core functions so there is no need to parse the data in order to access it.

These differences alone can be difficult to make a decision on which one to choose hence I decided to conduct an experiment to check the execution time between the two frameworks in a simple hello world app. In the following figure 2 it shows the app using ExpressJS framework.

Alt Text
Figure 2: ExpressJS Hello World App

As seen in the figure above, all this code does is that each time a you go through the route ‘/’ it prints ‘Hello World’ in the browser. Figure 3 below shows the same exact application but using Hapi instead.

Alt Text
Figure 3: Hapi Hello World App

As seen above, Hapi is abit more verbose than ExpressJS. In Hapi, the behaviour of the application is centered around the server object.

To get the execution time, I created a simple helper function called responseTimeHelper that calculated the timeTaken to execute the function in milliseconds. To help getting the time, I used the process object which is available globally in nodeJS so there is no need use require to get it. In NodeJS, many objects inside it emit events and process is among them. In Process.hrstime() if you don’t pass in any parameter it will return the current time in an array ie [seconds, nanoseconds]. But if you pass in the time then it can be used calculate the difference between the two and hence create a benchmark. This is what is done in this experiment, as for each framework used, we pass in the current time, and in the helper function we take the difference between after the function is executed and get the time taken to get a response as seen in Figure 4 below.

Alt Text
Figure 4: ResponseTime helper function

The results showed that it took 0.049262 milliseconds for ExpressJS to execute the function and it took 0.02584 milliseconds for Hapi to run the same application. Hence the difference isn’t that huge but as seen here Hapi is slightly faster than ExpressJS.

The following are the versions used in this experiment:
NodeJS: v12.18.2
Express: v4.17.1
Hapi: v20.0.0

Link to project https://github.com/gracesekwao/grace-collection/tree/benchmark

Top comments (0)