A framework that makes you hapi
From the very beginning, hapi was built around a simple philosophy-- development should be fun. The role of a framework is to increase your productivity and step in when you want it, but step aside when you dont. It should not mess around with your environment, nor should it take away any of the power available from the underlying platform. But more importantly, the role of a framework is to establish a foundation for collaboration.
Enterprise software development has earned the notoriety of a painful, bureaucratic, stale, and complicated process. Whats worse is that we have learned to accept it as the status quo. At the center of the problem is the fact that enterprise software development happens in large teams, often distributed organizationally and geographically, and require a significant amount of process to prevent one mistake from affecting the productivity of hundreds of developers, or worse affecting the business operations.
When I set course building hapi, I knew the framework had to be the best at handling the typical web application activities, such as authentication, state, and caching, but those are all prerequisites for just being a contender. The real core of the framework is its functionality aimed at making large scale development easier, and fun. It means paying attention to small details like protecting the developer from creating conflicts in routes, cookies or other named properties, and to big details like how to allow teams to work on the same application without constant coordination.
As the project matured, hapi itself became a large scale development process. With dozens of contributes from many different companies and a growing code base, we had to expand the reach of our hapi-ness centric philosophy to the full development process. We greatly value code quality and consistency, and since writing tests was usually a painful part of software development, we worked to make that better, streamlined, and enjoyable. We wrote new testing tools, fortified our coding style, and automated much of the tedious parts. We did the same for documentation, automating the process as much as possible to free developers to focus on functionality.
Four years into the project, the satisfaction of its community is still the greatest motivator and guide in moving the work forward. As it continue to mature, we learn more every day about how it is being used and how we can continue to evolve it; hapi is a dynamic community and the project has a history of not being afraid to break things in order to make them better. We refuse to accept the status quo, even our own.
I am excited you are taking the time to learn about hapi and consider it for your next project; hapi is a community project and you are now part of it. Your voice and your hapi-ness matter and you should consider this your very personal invitation to join us, ask questions, and share with us how we can make things better.
Eran Hammer
July 2015
Preface
Why use hapi?
A rich framework for building applications and services hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
-- hapijs.com
hapi is a web development framework designed for the rapid development of RESTful services APIs using JavaScript. While it is also simple and easy to use, hapi also includes a lot of features out-of-the-box that typically come from third-party tools, features like input validation, caching, authentication, etc.
Developers use hapi because it is easy to use, stable, robust, well-tested (and battle-tested by WalmartLabs during Black Friday), and powerful. It is specifically designed to work well in enterprise environments where multiple teams will work on individual or overlapping pieces that connect together. hapi does this by enabling developers to focus on writing reusable, modular code without forcing them to do so in a very narrowly defined way.
What should you know prior to reading this book?
Readers should be comfortable with JavaScript, Node.js, and the design and implementation of basic websites and web applications.
Source code/sample app
Throughout the book, we will be referencing code from our sample application: hapi-plugins.com. hapi-plugins.com is a searchable repository of hapi-related modules available on NPM. You can view the complete source code at https://github.com/hapijs-edge/hapi-plugins.com. You can see the completed application at https://hapi-plugins.com).
What will this book teach you?
This book will teach you how to build awesome web applications using hapi. It covers many aspects of how and why hapi works the way it does. You will see helpful and advanced features such as authentication, validation, caching, templating, testing, and debugging. In this book, you will also walk through the creation of a fully operational hapi web application.
Authors
Daniel Bretoi
Daniel Bretoi is a contractor @WalmartLabs where he worked on Eran Hammers team, which developed the hapi framework. He has worked in the industry for 15 years and favors interpreted languages. In 2002 he was an early adopter of Ruby and generally embraces new technologies.
Wyatt Preul
Wyatt Preul is a software developer for Walmart. He is one of the core maintainers of the hapi node.js framework. At Walmart, Wyatt was one of the investigators of the now infamous node.js memory leak. In addition to this, he has helped deliver many node.js projects within Walmart. Apart from his day job Wyatt loves all things node, web app security, archery, coffee roasting, and beer brewing.
Van Nguyen
Van Nguyen is a Senior Software Engineer, Full-Stack, IDEA (Big Data) at Intuit. He is currently hacking on JavaScript UIs for managing and visualizing Big Data.
Lloyd Benson
Lloyd Benson is a consultant at Walmart. He is also the owner of Open Source Infrastructure LLC.
Technical reviewers
We would like to the thank the following technical reviewers for their early feedback and careful critiques: Doron Pagot, Troy Whiteley, Jeffrey Jagoda, Colin Ihrig, Daniel Barnes, Michael Caputo, James Butler, Alvin Teh, Josh Mock, Michael Parks, Jonathan Holmlund, Glenn Jones, and Jesse Newcomer.
Chapter 1. Introduction to hapi
History
hapis origin would start at Yahoo! late in the year of 2010 on a projectcalled Sled. Sled was a collaborative list-making tool written as anexperiment to explore empowerment through restrictions by Eran Hammer. Sledwas written in node and Eran fell in love with the technology. Sled waseventually shut down and was generously open-sourced by Yahoo! as Postmile inAugust of 2011. Wanting to continue with node, Eran left for Walmart where hetook the good parts of Postmile and hapi was born on the 5th of August, 2011.Tasked with working on a web services layer, the development of hapi continuedat Walmart. The original versions in hapi used the Express framework.However, it was eventually realized that there were limitations with Expressthat made it not suitable for the particular requirements faced at Walmart.While Express was great, it was missing a lot of things and hapi evolved toits own standalone framework.