The Express Way VS The Hapi path

Doron Segal
4 min readFeb 13, 2017

I’m going to compare the two frameworks (spoiler alert: I’m not going to tell you which one is better because I don’t think there is a better framework. I have added a github repo with examples).

It is really up to the user to determine which framework is better.

Hapi come with more features out of the box compared to Express, meaning it contains more code which could be good or bad depending on what you are looking for.
When comparing the two frameworks by lines of code Express has total of 16,926 lines while Hapi has total of 30,914 lines — almost double (it means means nothing but still).

The EcoSystem
When using Hapi you will most likely will use Joi, Lout, Wreck, etc… This is an advantage Hapi has over Express. Having a whitelist of packages that you can use can make your app more performant and more secure.

Trusting a random node packages is risky! It can lead to bunch of issues and most importantly to an unsecured app. Express does not have this eco-system which can be risky when considering security and good practice.
However, Express offers you the flexibility to do whatever you want for good or worse.

Plugins VS MiddleWare
When using Hapi you mainly define routes and plugins (some may argue that you should use only plugins since they can act as route). The plugins are registered by the server and can be dependent on one another.

Hapi Plugin allow you to break your code into small isolated services where each of the plugin in charge on a specific business logic. It’s very useful when sharing your code with different services.

// load multiple plugins (server file)
server.register([require(‘myplugin’), require(‘yourplugin’)], (err) => {
if (err) {
console.error(‘Failed to load a plugin:’, err);
}
});
// myplugin.js - each plugin should export a `register` function and `attributes`
'use strict';
exports.register = function (server, options, next) {
next();
};
exports.register.attributes = {
pkg: require('./package.json')
};

On the other hand Express use middlewares which are sequence of functions, by executing a callback calling the next function.

//server.js
const express = require(‘express’);
const mw = require(‘./mw’);
const app = express();
app.get(‘/’,
mw.checkForAuthentication,
mw.logSomething, (req, res) => {
res.send(‘Hello World’);
});
//mw.js
module.exports = {
checkForAuthentication: (req, res, next) => {
// Check for user authentication and call next
next();
},
logSomething: (req, res, next) => {
console.log(req.path);
next();
}
};

In the example above it is easy to see how Express execute middlewares in order. When request is coming in to [GET] / mw.checkForAuthentication execute and than mw.logSomething and after will call res.send.
It’s look easy but required attention and self discipline when building a large application.

Events
Hapi have a built in event system, this is a great way for executing events on request, pre authentication and etc…

Security
Having a whitelist of packages and plugins you can use with Hapi make it a much more secure when there are plenty of packages out there. The importance of eco system really come to place when thinking about security.

Let’s compare two app with home page and login I’ll use a json file to mock our users database.

Example
I’ve created this repo for comparing the two frameworks, a simple app with authentication. I’ve used `ejs` as a template engine since most people are familiar with it.

Summary
I would like to start by saying that both frameworks are awesome and it’s to see that a huge effort was putting into both frameworks.
What should you use? it’s really depends… if you are a beginner and feel more comfortable with express just go with it! build your MVP and when you get more experience you can start looking into Hapi.js which is more robust and give more capabilities to the developers which make it harder to learn.

Why Hapi.js, if you’re leading a team, group or a CTO of a company you want to use a framework like Hapi.js. Hapi give developers the structure of how app should look like and where things should live. Jumping between two hapi apps is 10X easier than switching between express app due to the freedom and lack of structure express give to its developers.
Another good point is Plugins, Hapi let you design you app into small plugin and if needed you can just break them to a package publish it to npm and use it in multiple services.

If you find this useful or like it please press the heart it gives some fuel to keep writing better content, Thanks!

  • Doron :)

For more you posts checkout my blog

Github Repo: https://github.com/doron2402/express-vs-hapi

--

--

Doron Segal

Rational optimist, Dad, Tech founder, Environmentalist, CTO Founder @TryPerDiem.com