Hapi.js – Enterprise level backend framework from Walmart

Any application that can be written in Javascript, will eventually be written in Javascript.

Jeff Atwood – co-founder of StackOverflow

And that was in the era of first versions of jQuery (really quite a long time ago in coding business). That statement came almost 2 years before NodeJS was presented by it’s creator Ryan Dahl on JSConf event in middle 2009. This is where all started. NodeJS provides quite a lot of functionality, but trying to build an app on bare-bone NodeJS can become a trap pretty quickly. There is just too much stuff to think about and architect in a proper way for system not to fall apart when large team starts putting a lot of code on top of it. It just wasn’t possible. It was too expensive to do it that way.

A bit more than 1 year after NodeJS announcement frameworks started to appear and Express as the biggest one till this day appeared in 2009. Express run into problems when being used in large teams and for large projects. This was not enough for the Walmart which saw really big potential in NodeJS and really wanted a real enterprise level framework and was willing to invest millions of dollars in it. Eran Hammer was and is lead developer for new Walmart framework which will be opensourced. Eran Hammer was in the original team that built OAuth and after that a lead developer for OAuth2. The name for the framework came from Rene and Stimpy cartoon from song where they sing Happy Happy Joy Joy. The hat worn by Ren in this song is the actual logo of the Hapi framework itself. View it yourself in this video:

Critical moment for HapiJS framework was full deployment for mobile service for shopping in entire USA during Black Friday in November 2013. Node is perfect for heavy traffic and light processing, which was exactly what Walmart needed for their service. All eyes were pointed to HapiJS full-blown deployment, everybody just wanted it to fail and prove that NodeJS is not good enough for the enterprise. But surprise-surprise reality was completely the opposite. Official statement said: “The Walmart mobile servers built using Hapi were able to handle all mobile Black Friday traffic with about 10 CPU cores and 28GB RAM”. Followed by the comment from Eran Hammer: “100% of Walmart US mobile traffic is flowing through @nodejs using @hapijs and the servers are bored out of their mind. #nodebf”. This was crucial moment, NodeJS and HapiJS proved themselves. Also entire framework has 8 issues and 2 pull requests in the time of writing this article which is really small for such a large project.

Modularization is what React really brought to the table, if you have to choose one feature that defines the React philosophy it will be modularization. HapiJS is using exactly the same mindset, absolutely full modularization and separation which is for NodeJS framework really important especially when you have huge teams that work in parallel. Main goal of HapiJS is “Configuration over code” – to have just a config file put on top of the framework and that’s about it. This significantly reduces the complexity and practically defines well-thought frame in which you just put a standardized config and a bit of logic. When booting up the app, HapiJS will check if there are conflicting routes and will not start, so you will know right away if anything is wrong long time before production. If you have more similar routes, it will not go thru them in a ordered sequence, it will go from the most specific to least specific. Nowadays everything is modularized. If you want to change something in the framework you simply do not need to touch the framework itself, just use a plugin. There is a set of official modules which are listed at http://hapijs.com/plugins but the most important ones are: Joi – validation, Vision – templates, Catbox – caching, Boom – http friendly error, Poop – uncaught exceptions, Good – process monitoring, Bell – third party login, Lout – API documentation generator, Nes – websockets, Hapi-Auth-Basic – for auth, Scooter – user agent information, Inert – static file serving and so on.

If you want to create a website, you will use templateing plugin Vision and you can then use different templateing systems from Handlebars, Mustache… even React without problems. When you generate a page you can then use Catbox caching which you can plugin into MongoDB or Redis for storing cached versions for quick delivery. Caching is really smart and fast if it can’t deliver new version of the page within defined time of 300ms it will serve the from cache and continue to process and save new cached version for next user to consume. That’s really important for high traffic pages with little change over time. For validation there is excellent plugin Joi, which not only validates the data being sent to the server it will also validate the data coming out of the database using the same rules. This is good protection if data in database has been compromised. Validation is very detailed and precise allowing for fine grained control over validating each and every piece of data coming to and from database.

This framework really made near-perfect choices for building high-scale applications of today. The more you dig into it the more you like it and at the end you will be Hapi :)