Chapter 2. Building an API


This chapter covers

  • Building an API with hapi.js
  • Integrating business logic
  • Working with databases

You saw in chapter 1 that hapi apps are constructed by working with servers, routes, handlers, plugins, and your own business logic. In this chapter, you’ll see how those pieces come together. First I introduce a fictional brief for an API project that I’m going to build. Next I figure out the technical requirements and plan the API endpoints. Then I move on to implementing this in hapi step-by-step.

I won’t be diving too deeply into any single component of hapi yet. Instead, I’m going to take a more high-level approach, giving you the bigger picture of how apps in hapi are built.

2.1. Designing the API

This section introduces a fictional HTTP API project that will be the focus of this chapter. First I gather some requirements for the API. Once it’s clear how the API should work, I design the endpoints through which clients can access the API.

2.1.1. Your mission, should you choose to accept it

2.2. Getting set up

2.3. Retrieving and searching recipes

2.4. Writing maintainable code

2.5. Authentication

2.6. Creating and starring recipes

2.7. Summary