Chapter 6. Building APIs

 

This chapter covers

  • Using Express to build an API
  • HTTP methods and how they respond to common CRUD operations
  • Versioning your API using Express’s routers
  • Understanding HTTP status codes

Friends, gather round. This chapter marks a new beginning. Today, we exit the abstract but critical core Express and enter the real world. For the rest of this book, we’ll be building much more real systems atop Express. We’ll start with APIs.

API is a pretty broad term. It stands for application programming interface, which doesn’t demystify the term much. If it were up to me (obviously it isn’t), I’d rename it something like software interface. A UI is meant to be consumed by human users, but a software interface is meant to be consumed by code. At some level, all UIs sit on top of software interfaces—that is, on top of some APIs.

At a high level, APIs are ways for one piece of code to talk to another piece of code. This could mean a computer talking to itself or a computer talking to another computer over a network. For example, a video game might consume an API that allows the code to draw graphics to the screen. You’ve seen a few methods available in the Express API, like app.use or app.get. These are interfaces that you as a programmer can use to talk to other code.

6.1. A basic JSON API example

6.2. A simple Express-powered JSON API

6.3. Create, read, update, delete APIs

6.4. API versioning

6.5. Setting HTTP status codes

6.6. Summary