Table of Contents for
Node.js 8 the Right Way

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Node.js 8 the Right Way by Jim Wilson Published by Pragmatic Bookshelf, 2018
  1. Title Page
  2. Node.js 8 the Right Way
  3. Node.js 8 the Right Way
  4. Node.js 8 the Right Way
  5. Node.js 8 the Right Way
  6.  Acknowledgments
  7.  Preface
  8. Why Node.js the Right Way?
  9. What’s in This Book
  10. What This Book Is Not
  11. Code Examples and Conventions
  12. Online Resources
  13. Part I. Getting Up to Speed on Node.js 8
  14. 1. Getting Started
  15. Thinking Beyond the web
  16. Node.js’s Niche
  17. How Node.js Applications Work
  18. Aspects of Node.js Development
  19. Installing Node.js
  20. 2. Wrangling the File System
  21. Programming for the Node.js Event Loop
  22. Spawning a Child Process
  23. Capturing Data from an EventEmitter
  24. Reading and Writing Files Asynchronously
  25. The Two Phases of a Node.js Program
  26. Wrapping Up
  27. 3. Networking with Sockets
  28. Listening for Socket Connections
  29. Implementing a Messaging Protocol
  30. Creating Socket Client Connections
  31. Testing Network Application Functionality
  32. Extending Core Classes in Custom Modules
  33. Developing Unit Tests with Mocha
  34. Wrapping Up
  35. 4. Connecting Robust Microservices
  36. Installing ØMQ
  37. Publishing and Subscribing to Messages
  38. Responding to Requests
  39. Routing and Dealing Messages
  40. Clustering Node.js Processes
  41. Pushing and Pulling Messages
  42. Wrapping Up
  43. Node.js 8 the Right Way
  44. Part II. Working with Data
  45. 5. Transforming Data and Testing Continuously
  46. Procuring External Data
  47. Behavior-Driven Development with Mocha and Chai
  48. Extracting Data from XML with Cheerio
  49. Processing Data Files Sequentially
  50. Debugging Tests with Chrome DevTools
  51. Wrapping Up
  52. 6. Commanding Databases
  53. Introducing Elasticsearch
  54. Creating a Command-Line Program in Node.js with Commander
  55. Using request to Fetch JSON over HTTP
  56. Shaping JSON with jq
  57. Inserting Elasticsearch Documents in Bulk
  58. Implementing an Elasticsearch Query Command
  59. Wrapping Up
  60. Node.js 8 the Right Way
  61. Part III. Creating an Application from the Ground Up
  62. 7. Developing RESTful Web Services
  63. Advantages of Express
  64. Serving APIs with Express
  65. Writing Modular Express Services
  66. Keeping Services Running with nodemon
  67. Adding Search APIs
  68. Simplifying Code Flows with Promises
  69. Manipulating Documents RESTfully
  70. Emulating Synchronous Style with async and await
  71. Providing an Async Handler Function to Express
  72. Wrapping Up
  73. 8. Creating a Beautiful User Experience
  74. Getting Started with webpack
  75. Generating Your First webpack Bundle
  76. Sprucing Up Your UI with Bootstrap
  77. Bringing in Bootstrap JavaScript and jQuery
  78. Transpiling with TypeScript
  79. Templating HTML with Handlebars
  80. Implementing hashChange Navigation
  81. Listing Objects in a View
  82. Saving Data with a Form
  83. Wrapping Up
  84. 9. Fortifying Your Application
  85. Setting Up the Initial Project
  86. Managing User Sessions in Express
  87. Adding Authentication UI Elements
  88. Setting Up Passport
  89. Authenticating with Facebook, Twitter, and Google
  90. Composing an Express Router
  91. Bringing in the Book Bundle UI
  92. Serving in Production
  93. Wrapping Up
  94. Node.js 8 the Right Way
  95. 10. BONUS: Developing Flows with Node-RED
  96. Setting Up Node-RED
  97. Securing Node-RED
  98. Developing a Node-RED Flow
  99. Creating HTTP APIs with Node-RED
  100. Handling Errors in Node-RED Flows
  101. Wrapping Up
  102. A1. Setting Up Angular
  103. A2. Setting Up React
  104. Node.js 8 the Right Way

Simplifying Code Flows with Promises

In this section, you’ll add another API to your lib/search.js file. Like the /search/books API from last section, this /suggest API will hit your Elasticsearch cluster for information, but this time we’ll use Promises rather than callbacks to manage asynchronous control flow.

Fulfilling Promises

To understand Promises, it helps to start with a brief review of JavaScript code flow and the mechanisms for structuring it. Whenever a regular JavaScript function starts executing, it will finish in one of two ways: either it will run to completion (success) or it will throw an exception (failure).

For synchronous code this is good enough, but for asynchronous code we need a bit more. The Node.js core module callbacks use two arguments to reflect these two cases; e.g., (err, data) => {...}. And EventEmitters use different event types (like data and error) to distinguish success and failure modes.

Promises offer yet another way to manage asynchronous results. A Promise is an object that encapsulates these two possible results (success and failure) for an operation. Once the associated operation has completed, the Promise will either be resolved (success case) or rejected (error case). When using a Promise, you attach callback functions for these cases using .then() and .catch(), respectively.

Let’s take a look at a quick example.

 const​ promise = ​new​ Promise((resolve, reject) => {
 // If successful:
  resolve(someSuccessValue);
 // Otherwise:
  reject(someErrorValue);
 });

Here we’re creating a new Promise, passing in an anonymous callback function that is invoked immediately. The resolve and reject parameters are callback functions you should invoke to resolve or reject the Promise, respectively. The value you pass to these functions will be sent forward to any .then() or .catch() handlers.

Let’s add those next.

 promise.then(someSuccessValue => { ​/* Do something on success. */​ });
 promise.​catch​(someErrorValue => { ​/* Do something on failure. */​ });

Now, whenever the Promise is settled, the appropriate callback will be invoked. Importantly, it doesn’t matter whether the Promise has already been settled when you attach handlers with .then() and .catch(). If the Promise has already been settled, those handlers will be called right away. But if the Promise has yet to be settled, then those handlers will wait to be called when it is. Contrast this with a typical EventEmitter—if you attached your .on(’error’) handler too late, you’ll simply miss out on handling the event.

A Promise can only ever be settled once. That is, once a Promise has been resolved or rejected, it can’t be resolved or rejected again. This doesn’t stop you from attaching more callbacks using .then() or .catch(), but it does guarantee that any particular callback added this way will be invoked at most once. Once again, contrast this with a typical EventEmitter, which may emit many data or error events.

That’s enough theory for now. Let’s see Promises in action.

Using a Promise with request

Start by adding the following shell code to your lib/search.js below the previous /search/books API to create the /suggest API endpoint.

 /**
  * Collect suggested terms for a given field based on a given query.
  * Example: /api/suggest/authors/lipman
  */
 app.​get​(​'/api/suggest/:field/:query'​, (req, res) => {
 });

As with the /searchi/books API, this API takes two parameters: the :field for which we want suggestions and the :query to suggest from.

Now, inside the shell we need to construct the Elasticsearch request body and the rest of the options to send through request.

 const​ esReqBody = {
  size: 0,
  suggest: {
  suggestions: {
  text: req.params.query,
  term: {
  field: req.params.field,
  suggest_mode: ​'always'​,
  },
  }
  }
 };
 
 const​ options = {url, json: ​true​, body: esReqBody};

This request body is designed to trigger Elasticsearch’s Search Suggesters feature.[69] Setting the size parameter to zero informs Elasticsearch that we don’t want any matching documents returned, just the suggestions.

Elasticsearch’s Suggest API allows you to request multiple kinds of suggestions in the same request, but here we’re submitting only one. If the request succeeds, we expect to get back a JSON object that contains something like the following, which resulted from an authors search for the string lipman:

 {
 "suggest"​: {
 "suggestions"​: [
  {
 "text"​: ​"lipman"​,
 "offset"​: 0,
 "length"​: 6,
 "options"​: [
  {
 "text"​: ​"lilian"​,
 "score"​: 0.6666666,
 "freq"​: 26
  },
  {
 "text"​: ​"lippmann"​,
 "score"​: 0.6666666,
 "freq"​: 5
  },
 // ...
  ]
  }
  ]
  }
 }

To get this result, let’s use request like we did for the /search/books API, but this time we’ll use a Promise. Add the following code after creating the options object.

 const​ promise = ​new​ Promise((resolve, reject) => {
  request.​get​(options, (err, esRes, esResBody) => {
 
 if​ (err) {
  reject({error: err});
 return​;
  }
 
 if​ (esRes.statusCode !== 200) {
  reject({error: esResBody});
 return​;
  }
 
  resolve(esResBody);
  });
 });
 
 promise
  .then(esResBody => res.status(200).json(esResBody.suggest.suggestions))
  .​catch​(({error}) => res.status(error.status || 502).json(error));

This code proceeds in two parts. First we create the Promise, then we attach callbacks to it.

In the Promise-creation part, we call out to request just like in the previous API. But this time, instead of handling the results directly, we either reject or resolve the Promise.

The two rejection cases are the same as before. If the Elasticsearch cluster is unreachable, then the err object will be populated. On the other hand, if the request is malformed, then the err object will be null but the statusCode will be something other than 200 OK. In both cases, we want to reject the Promise so that any .catch() handlers are invoked.

Notice that the object passed to reject has a single key called error that includes the error details. This is a design choice that I’ll explain in a bit. When calling reject for a Promise, you’re free to give it any value you like (or none at all).

In the event that Elasticsearch was reachable and returned a 200 OK status code, we resolve the Promise.

Once the Promise has been created, we attach a .then() and a .catch() handler. The .then() handler sets the Express response status to 200, then extracts and serializes the suggest.suggestions object returned from Elasticsearch.

The .catch() handler extracts the .error property of the object provided. It does this using the destructuring assignment technique we used earlier when extracting the _source documents in the /search/books API. Inside the callback, we use the error object to set the response status code and body.

Save your lib/search.js if you haven’t already. To review, your /suggest API code should look like the following:

 /**
  * Collect suggested terms for a given field based on a given query.
  * Example: /api/suggest/authors/lipman
  */
 app.​get​(​'/api/suggest/:field/:query'​, (req, res) => {
 
 const​ esReqBody = {
  size: 0,
  suggest: {
  suggestions: {
  text: req.params.query,
  term: {
  field: req.params.field,
  suggest_mode: ​'always'​,
  },
  }
  }
  };
 
 const​ options = {url, json: ​true​, body: esReqBody};
 
 const​ promise = ​new​ Promise((resolve, reject) => {
  request.​get​(options, (err, esRes, esResBody) => {
 
 if​ (err) {
  reject({error: err});
 return​;
  }
 
 if​ (esRes.statusCode !== 200) {
  reject({error: esResBody});
 return​;
  }
 
  resolve(esResBody);
  });
  });
 
  promise
  .then(esResBody => res.status(200).json(esResBody.suggest.suggestions))
  .​catch​(({error}) => res.status(error.status || 502).json(error));

Once you save the file, nodemon should pick up the changes and restart the service automatically. Now we can hit the service with curl and jq.

First, let’s try finding author suggestions for the string lipman.

 $ ​​curl​​ ​​-s​​ ​​localhost:60702/api/suggest/authors/lipman​​ ​​|​​ ​​jq​​ ​​'.'
 [
  {
  "text": "lipman",
  "offset": 0,
  "length": 6,
  "options": [
  {
  "text": "lilian",
  "score": 0.6666666,
  "freq": 26
  },
  {
  "text": "lippmann",
  "score": 0.6666666,
  "freq": 5
  },
  {
  "text": "lampman",
  "score": 0.6666666,
  "freq": 3
  },
  {
  "text": "lanman",
  "score": 0.6666666,
  "freq": 3
  },
  {
  "text": "lehman",
  "score": 0.6666666,
  "freq": 3
  }
  ]
  }
 ]

If you see this, great! Things are working as expected.

Of course, you can use jq to extract just the text of the suggestions if you like.

 $ ​​curl​​ ​​-s​​ ​​localhost:60702/api/suggest/authors/lipman​​ ​​|​​ ​​jq​​ ​​'.[].options[].text'
 "lilian"
 "lippmann"
 "lampman"
 "lanman"
 "lehman"

Next, let’s look at a module called request-promise, which streamlines the use of Promises with the request module for issuing requests.

Replacing request with request-promise

Recall in the last section, where we created a Promise to abstract out the fulfillment of the asynchronous request from the handling of the success and failure cases. In practice, you won’t usually use new Promise() to create a Promise for a bit of asynchronous functionality. It’s considerably more common for Promises to be created by factory methods.

For example, you can use the static method Promise.resolve to create a new Promise and immediately resolve it.

 Promise.resolve(​"exampleValue"​)
  .then(val => console.log(val)); ​// Logs "exampleValue".

In the case of request, there’s a module called request-promise that wraps the various request methods to return Promises instead of taking a callback. To use it, start by installing it with npm.

 $ ​​npm​​ ​​install​​ ​​--save​​ ​​--save-exact​​ ​​request-promise@4.1.1

Next, add a require line to the top of your lib/search.js to pull it in as a constant named rp.

 const​ rp = require(​'request-promise'​);

Now it’s a drop-in replacement for the Promise-creating code we used in the previous section. This simplifies the whole block down to just this:

 rp({url, json: ​true​, body: esReqBody})
  .then(esResBody => res.status(200).json(esResBody.suggest.suggestions))
  .​catch​(({error}) => res.status(error.status || 502).json(error));

This is the reason that I elected to wrap the rejection value in an object whose .error property contained the error—so that we could use request-promise as a replacement.

For the rest of the APIs in this chapter, we’ll use request-promise rather than regular request. Now let’s move on to building out the API endpoints for working with book bundles. Unlike the /search/books and /suggest APIs, which only retrieved data from Elasticsearch, the bundle APIs will need to create, update, and delete records as well.