Hapi vs Express vs Koa

Last Updated on: July 5, 2022

Hapi vs Express vs Koa

It’s been 10 years since NodeJS came into existence and became one of the fastest-growing open-source projects, with 63,000+ stars on GitHub. The main advantage of NodeJS is ‘JS’. It uses the Javascript syntax, giving us the advantage of the same language on both the frontend and the backend.
In this 10-year life span of NodeJS, many NodeJS frameworks have been built. These frameworks help us to enhance productivity, reduce boilerplate efforts and improve the scalability of NodeJS. Let’s understand which is the best NodeJS Framework:

Introduction

Let’s explore web development with Node: Hapi vs Express vs  Koa, its most popular frameworks. The purpose of this article is to look at how each of these NodeJS Frameworks is defined, their popularity and performance. Understand how difficult or easy it is to write a basic Hello World Program. Based on all of these factors, we will draw up the strengths and weakness and conclude on which is the best NodeJS Framework today!

Express.Js

According to https://expressjs.com/, web development with Node and ExpressJS framework is a minimal and flexible web application framework that provides a robust set of features for web and mobile applications. ExpressJS framework doesn’t really offer any out of the box features.

Koa

Based on https://koajs.com/, Koa’s official website, Koa aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Koa aims to leverage async functions, enabling better error-handling. 

Koa is a more minimal framework than ExpressJS framework having around 2K LOC in the source code and by default supports relatively new JavaScript features like generators and async/await.

Hapi

According to https://hapi.dev/, Hapi is a rich framework and allows developers to focus on writing reusable application code instead of spending time building infrastructure.

Hapi is loaded out-of-the-box, with the most mature and complete set of extension points at every step — including authentication, authorization, and validation.

Popularity

Popularity is an important aspect of any technology or framework. Not because it feels good to be working on a popular framework, but because it determines the support and community of it. Let’s take a look at some matrices for the best NodeJS Frameworks: Hapi vs Express vs Koa.

matrices for the best NodeJS Frameworks: Hapi vs Express vs Koa by Systango

This Data was taken from Github and NPM

Looking at the above figures, web development with node and ExpressJS Framework is definitely the most popular. This is also because some popular NodeJS frameworks like hapiJS and loopbackJS are built on top of ExpressJS Framework.

Performance – Hapi vs Express vs Koa

Performance is a crucial aspect of any application development. No matter how easy or good looking the code is, how strong the community is, if the NodeJS framework does not meet the performance expectations, the developer will switch to another NodeJS framework that does. It does not mean that we have to choose the best NodeJS framework all the time, but it has to have a balance of ease of development and performance.

Here’s a 2017 study on the performance of Hapi vs Express vs Koa and a few other NodeJS frameworks:

2017 study on the performance of Hapi vs Express vs Koa and a few other NodeJS frameworks by Systango

It clearly shows that KoaJS came out on top whereas HapiJS performed the worst between the NodeJS Frameworks: KoaJS, HappiJS, and ExpressJS.

A simple “Hello World” app has the following performance in terms of requests per second:

  • Hapi       – 29998 req / sec
  • Koa        – 50933 req / sec
  • Express – 38510 req / sec

Here as well, koa is far better than the other two frameworks with bare minimum functionality.

Hapi vs Express vs Koa – Installation And Basic Hello World application

Let’s see how to install these NodeJS frameworks and get a basic hello world application up and running.

NodeJS should be pre-installed into your system. For web development with node and Express or Hapi, you can use any version of NodeJS but for Koa, your NodeJS version should be 7 or higher.

Express
Installation
The installation command for web development with node and ExpressJS Framework:
$ npm install express

Building the ‘Hello World’ app

Web development with node and express requires you to the express package, initialize the express app to the app variable and call the app.listen() method, which is the wrapper around the native NodeJS Http.createServer() method.

const express = require(‘express’)
const app = express()
const port = 3000
app.get(‘/’, (req, res) => res.send(‘Hello World!’))
app.listen(port, () => console.log(`Port on which the app is listening ${port}!`))

And that’s it. Our ‘Hello World’ application is ready.

Koa

Web development with node and express and Koa is built on a similar philosophy. Therefore, you will see a lot of similarities in the way code is written in Express vs Koa.

Installation
The installation command for koa:
$ npm install koa

Building the ‘Hello World’ app

The basic hello world app will look like this in koa:

const Koa = require(‘koa’);
const app = new Koa();
app.use(async ctx => {
  ctx.body = ‘Hello World’;
});
app.listen(3000);

Application on Koa listens on the port 3000. For other paths, the server will display a 404 message.

Hapi

Installation
The installation command for hapiJS:
$ npm install hapi

Building the ‘Hello World’ app

The basic hello world app will look like this in hapiJS

let Hapi = require(‘hapi’);
let server = new Hapi.Server(3000);
server.route({
  method: ‘GET’,
  path: ‘/’,
  handler: function(request, reply) {
      reply(‘Hello world’);
  }
});
server.start(function() {
  console.log(‘Hapi is listening to http://localhost:3000’);
});

Here we are using the built-in method that the server object provides us server.route() which has the following options: path, HTTP method, and handler function.

This app like on Koa will listen on the port 3000 and respond with 404 for any other path.

Now we know the basic setup and routing, what the code looks like and the efforts needed to build a REST API. But that doesn’t help us understand which is the best NodeJS framework. Let’s now look at the performance of these NodeJS Frameworks:

WHY SYSTANGO?

Selecting a tech stack for your new project is often an overwhelming decision if you are non-technical, and they come with a fear that committing to the wrong language or framework will have serious consequences down the road. Don’t worry!
Systango can help you choose the right tech stack based on your project needs.
Your First Consultation is on Us!

Reach Us Software Development Company | Systango

The Goods – Hapi vs Express vs Koa


Express
  1. Simple, Scaleable
  2. Biggest community
  3. Fully customisable
  4. Faster Development
  5. Small learning curve
Koa
  1. Very lightweight
  2. Cleaner, more readable async code
  3. ES6 support out of the box
  4. No more callback hell, Generator based flow
Hapi
  1. More developer-friendly
  2. Provides enterprise-level plugins such as joi, yar, catbox, boom, tv, and travelogue
  3. Backed by WalmartLabs as well as many other big companies using Hapi in production, so better tested
  4. Has a plugin-based architecture for scaling
  5. Great documentation
  6. Plugins for almost all standard functionalities like caching, authentication, and input validation

The Bads – Hapi vs Express vs Koa


Express
  1. For large codebase refactoring becomes very challenging
  2. Many ways to do the same thing
  3. No built-in error handling
  4. A lot of manual labour is required
Koa
  1. Pretty much new compared to other NodeJS frameworks
  2. Small community
Hapi
    1. A developer needs to use Hapi-specific modules
    2. Architecture requires additional learning for developers

Conclusion

At Systango, we have realised that all the NodeJS frameworks have some benefits over the other and no individual framework can be declared as Best NodeJS Framework. Based on the need for the application, our developers choose which framework to use based on the balance of performance, ease of development, community support or some other factor.

Web development with Node and Express is definitely the most popular and recognised of the three. It might be a better choice for rapid prototyping or highly-customised projects. The number of ExpressJS/Connect middleware modules is vast. It provides a balance between ease of code with better support for third party integrations and performance.

Why Systango?

We offer end to end Node.JS development, consulting, migration, and team augmentation support for both web and mobile application development. If you are looking for Node experts. Get in touch with us. Save your time & money without compromising on quality!
Get in Touch Today!

Reach Us Software Development Company | Systango

But Koa or Hapi are presenting themselves as strong alternatives. Koa is the upcoming framework that’s ahead of the pack. It has embraced ES6 and the web component ideology that the web development component is following. It’s more updated and has the best performance. It can be well suited for microservices, building small services very quickly with a promise of performance.

If you are looking at large teams and large projects, Hapi is the way to go. It’s focussed on configuration over code which is very beneficial for teams and re-usability. It seems to be a good choice for monolith applications, with a smooth way of scaling the code and less code maintenance.

Surbhi Khandelwal

March 24, 2020

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *