# Introduction

# About Learn Hapi/MySQL

Learn Hapi/MySQL book will introduce you to basic, yet comprehensive usage of Hapi (a NodeJS framework) and MySQL (a relational database engine) for creating REST API.

Some of the basic Hapi features that you will learn are creating a server, adding route endpoints, responding to user's requests, validating input, error handling, authentication, working with MySQL database, and much more.

Once you become familiar with some of these concepts, you will feel comfortable creating your own database-powered NodeJS backend API, Insya Allah.

This book basically is a living documentation of my own journey on learning Hapi. By sharing it with the public, I hope people find it useful and can help them learning Hapi/MySQL too.

To make this book more interesting, fun, and easy to read, I will present Learn Hapi/MySQL through a story of a person learning Node backend development. The person is John Joe — an ambitious man who determined to build a software as a service app.

# Disclaimer

To be clear, I am not a Hapi/MySQL expert. I happen to love them. But, my understanding of these two technologies is not deep. In fact, I'm still learning them. So, please do understand that whatever I tell you in this book comes from a fellow learner's perspective, not as a Hapi or MySQL expert.

# Who is this book for?

This book was geared toward post beginner developers looking to get up and running on learning their first NodeJS framework, especially Hapi.

# Prerequisites

This book assumes an intermediate level of JavaScript. What do I mean as intermediate is that you've got Javascript fundamental covered and getting comfortable at using terminal and NPM. Prior experience with Node development or other Node frameworks is beneficial but is not required.

So If you are totally new to Javascript, it might not wise to dive right into learning Hapi/MySQL - understand the basics then come back!

And technically, you should have the following programs installed:

  • Node
  • NPM
  • MySQL

# Background Story

After getting comfortable with writing Javascript for the frontend, Joe decided that he should learn Node backend development too so he could become a full-stack Javascript developer. That way, he can independently create an API to power his frontend app.

Fortunately, Joe already has some familiarity with a particular database, namely MySQL. He learned it a long time ago from a course Intro to SQL: Querying and Managing Data (opens new window) that is available for free at Khan Academy. Yes, he knew that, from his research, the Javascript community actually prefers MongoDB, however, he wants to use his existing knowledge so he could get up and running as soon as possible. So for now, he will stick to MySQL until he finds a good reason to switch to others.

And for the Node framework, he chose HapiJS.

# What and Why Hapi?

So what is Hapi and why Joe chose it?

Here is what Joe found in his research.

On Hapi's official site (opens new window). Joe found these:

  • "The Simple, Secure Framework Developers Trust"
  • "Build powerful, scalable applications, with minimal overhead and full out-of-the-box functionality"
  • "Originally developed to handle Walmart’s Black Friday scale."

"If it's simple then I'll be able to understand it easily. If it's secure then it would save me a lot of trouble on a security matter that I am clueless about. If it's full out-of-the-box functionality then I don't need to deal with much third-party library. And if it can handle Walmart's Black Friday, then surely it can handle my simple app!", John said to himself.

Joe also found these convincing arguments from DWYL's Learn Hapi repo (opens new window):

  • WalmartLabs are the guys who found/solved the Node.js CORE Memory Leak; they have developed Hapi following Benchmark Driven Development and the result is a high-performance framework
  • The Lead Developer of Hapi is Eran Hammer who was one of the original authors of the OAuth (Secure Authentication) Spec. He has built a security-focussed mindset into Hapi and reviews all code included in Hapi. Several members of the Node Security Project are core contributors to Hapi which means there are many security-minded eyes on the code.

So that's Hapi and why Joe chose it.

# The Mission

Joe decided that he should build a point of sale app. The reason is that he wanted to modernize his wife's retail store. Untill now, the store's transaction was still recorded using pen and paper. For some basic usage it's pretty convenience. However, when it cames to search and analyze the transaction, it's a nightmare!

In order to be useful to power his wife's store digitally, Joe thought that the app should have the following features:

  • Recording sales (obviously)
  • Ability to sell on credit
  • Applying discount
  • Creating multiple stores
  • Tracking inventory
  • Transfer order
  • Purchase order
  • Stock Adjustment
  • Multiple roles: owner & employee

Also Joe wanted his app to have the following features so he can offer the app to other store owner and make money from it:

  • Offering subscription plan
  • Accepting payment

Then Joe began his research on how to make that kind of app. Soon he realized that the app is not trivial. It requires a comprehensive learning of backend development. At least he should be able to do the following tasks to turn his idea into reality:

  • Creating server
  • Defining endpoints of various HTTP methods
  • Handling user request
  • Defining model
  • Working with database
  • Authentication with JWT
  • Authorization

After defining the goals, now Joe is ready to embark on his first journey on learning HapiJS.