Creating web services is what Node.js was made for, and that’s still a dominant use case for it. In this chapter, we’ll continue the multipart journey to build out a web application, this time by implementing RESTful web services.
It’s a long chapter, not because the code is particularly verbose, but because the concepts deserve particular attention. We’ll cover all of these topics on the way:
Node.js 8 is the first long-term support (LTS) version to include async functions, a recent ECMAScript feature. Unlike regular functions that run to completion, async functions allow you to effectively suspend execution to await an asynchronous result. You’ll use async functions to dramatically simplify programming sequences of asynchronous events.
You’ll develop RESTful APIs using Express, backed by Elasticsearch. You’ll learn about Express middleware, how to write route handlers, and how to harness Elasticsearch’s query APIs. We’ll also review and reinforce HTTP methods and status codes for communicating to your services’ users.
A Promise is a special kind of object that presents a unified way of dealing with synchronous and asynchronous code flows. You’ll use Promise-producing factory methods for issuing HTTP requests. You’ll take advantage of destructuring assignment and computed property names to streamline data definitions.
Configuring your service shouldn’t be an exercise in frustration. You’ll learn how to use the nconf module for organizing configuration options. You’ll also use nodemon to monitor Node.js programs and automatically restart them when the source changes.
Node.js comes with support for low-overhead HTTP servers out of the box using the http module.[64] But writing services against the low-level http module directly can be a lot of work. So we’ll use Express for developing our web services.[65]