So you want to build a web application and you need a framework to help you do that. You’ve heard about Express but are there other options are out there? Exactly how many frameworks exist and what are the differences between them?
The answer to that last question is there’s a lot, with significant differences when it comes to syntax, capability, scalability and more. In this article I am going to briefly compare the most popular framework, Express, with a smaller alternative called Hapi.
Please note that I am nothing close to an expert on frameworks; This article is for my own research and reflection as much as anyone else’s. Let’s get into it:
What exactly is a Framework and why is it useful?
A web development framework is a set of resources and tools for software developers to build and manage web applications. Frameworks take care of complex, commonly used functions, such as server-client communication, so that you don’t have to devote massive amounts of time and resources to coding it all manually.
Express.js is a minimal and flexible Node.js framework, developed by TJ Holowaychuk, which provides a powerful set of features for web and mobile applications. Many beginners are more comfortable using Express because it is very straightforward and it favors a similar design pattern to developing a native Node.js application. Additionally, Express is the most popular framework, and as a result there is much documentation, and other resources, available online to help junior devs quickly grasp the ins and outs.
All endpoints must be manually created in Express, which could be quite time consuming, and as your codebase grows in size, refactoring could become a challenge. Organization needs to be very clear to avoid problems when maintaining the code.
In this context, middleware is essentially code defined in between the requests and responses made to a web server. (get it? It’s in the middle.) This code has access to both the request and response objects and can modify that data, ultimately returning the correct rendering of a page to the client. Express itself behaves like a middleware to help manage servers and routes. Both frameworks I am reflecting on implement middleware in their own way, making for slightly different user experiences.
Hapi is short for Http API. It was developed by WalmartLabs. Yes, that Walmart. It was initially built to handle the scaling traffic of their Black Friday sales. For this reason, the codebase is well tested and maintained, and is designed to scale up very efficiently. The framework encourages reusability of existing code and is designed with business applications in mind. As of 2014, any claim Walmart had to Hapi ended. It is still used heavily within Walmart, but now anyone and everyone may freely, legally use it as well.
Unlike Express, Hapi by itself contains methods for processing form data, parsing JSON, error handling, setting cookies and more. Making it theoretically more capable right “out of the box”.
But what happens if you need functionality other than what Hapi offers by default? Well, where Express uses middleware to extend Node’s request/response objects, Hapi uses an extensive collection of Hapi-specific “plugins”. Generally the same concept, but there is a much smaller library to choose from.
As we can see, Hapi has more boilerplate code, but it is clearer to read and understand, and as the project grows in size and complexity, it is actually easier to work with.
So what’s the conclusion?
Both frameworks have a lot in common. Both can create a server with just a few lines of code and both make creating a REST API very simple. Which framework you might be more inclined to use depends on your specific project’s scale, requirements and what sort of team will be developing it. Each framework has it’s own benefits and disadvantages which may make it the superior choice for your web application.
If you’re looking for something standard, simple and stable with a lot of support, and a generally easier learning curve, then Express is definitely a good choice. If you are bigger more complex applications, which need to scale and be understood by large teams, Hapi may be superior.
At the end of the day, both are very capable frameworks and given enough time and knowledge, a skilled dev could accomplish the same objectives, regardless of which one is used.
Thanks for reading!