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

Responding to Requests

The REQ/REP pattern is quite common in networked programming, particularly in Node. We’ll use this pattern often in the next couple of chapters, and ØMQ has great support for it. As you’ll see in a minute, this is where the “Q” of ØMQ becomes apparent.

In ØMQ, a REQ/REP pair communicates in lockstep. A request comes in, then a reply goes out. Additional incoming requests are queued and later dispatched by ØMQ. Your application, however, is aware of only one request at a time.

Let’s see how this works, again using the filesystem as a source of information for building a microservice. In this scenario, a responder waits for a request for file data, then serves up the content when asked. We’ll start with the responder—the REP (reply) part of the REQ/REP pair.

Implementing a Responder

Open an editor and enter the following:

 'use strict'​;
 const​ fs = require(​'fs'​);
 const​ zmq = require(​'zeromq'​);
 
 // Socket to reply to client requests.
 const​ responder = zmq.socket(​'rep'​);
 
 // Handle incoming requests.
 responder.on(​'message'​, data => {
 
 // Parse the incoming message.
 const​ request = JSON.parse(data);
  console.log(​`Received request to get: ​${request.path}​`​);
 
 // Read the file and reply with content.
  fs.readFile(request.path, (err, content) => {
  console.log(​'Sending response content.'​);
  responder.send(JSON.stringify({
  content: content.toString(),
  timestamp: Date.now(),
  pid: process.pid
  }));
  });
 
 });
 
 // Listen on TCP port 60401.
 responder.bind(​'tcp://127.0.0.1:60401'​, err => {
  console.log(​'Listening for zmq requesters...'​);
 });
 
 // Close the responder when the Node process ends.
 process.on(​'SIGINT'​, () => {
  console.log(​'Shutting down...'​);
  responder.close();
 });

Save the file as zmq-filer-rep.js. The program creates a ØMQ REP socket and uses it to respond to incoming requests.

When a message event happens, we parse out the request from the raw data. Next we call fs.readFile to asynchronously retrieve the requested file’s content. When it arrives, we use the responder’s send method to reply with a JSON serialized response, including the file content and a timestamp. We also include the process ID (pid) of the Node.js process in the response.

The responder binds to TCP port 60401 of the loopback interface (IP 127.0.0.1) to wait for connections. This makes the responder the stable endpoint of the REP/REQ pair.

Since this service reads content from your filesystem and serves it to any requester, be sure to set the IP to 127.0.0.1 (localhost)! This is for demonstration purposes only.

Finally, we listen for SIGINT events on the Node.js process. This Unix signal indicates that the process has received an interrupt signal from the user—typically invoked by pressing Ctrl-C in the terminal. The clean thing to do in this case is ask the responder to gracefully close any outstanding connections.

Start the program in a terminal as usual:

 $ ​​node​​ ​​zmq-filer-rep.js
 Listening for zmq requesters...

Looks like our responder is ready! But to connect to it, we’ll need to develop a client, so let’s put one together.

Issuing Requests

Creating a requester to work with our responder is pretty short. Open an editor and enter this:

 'use strict'​;
 const​ zmq = require(​'zeromq'​);
 const​ filename = process.argv[2];
 
 // Create request endpoint.
 const​ requester = zmq.socket(​'req'​);
 
 // Handle replies from the responder.
 requester.on(​'message'​, data => {
 const​ response = JSON.parse(data);
  console.log(​'Received response:'​, response);
 });
 
 requester.connect(​'tcp://localhost:60401'​);
 
 // Send a request for content.
 console.log(​`Sending a request for ​${filename}​`​);
 requester.send(JSON.stringify({ path: filename }));

Save the file as zmq-filer-req.js.

This program starts off by creating a ØMQ REQ socket. Then we listen for incoming message events and interpret the data as a JSON serialized response (which we log to the console). The end of the program kicks off the request by connecting to the REP socket over TCP and finally calling requester.send. The JSON request message contains the requested file’s path as specified on the command line.

Let’s see how these REQ and REP sockets work together. With the zmq-filer-rep program still running in one terminal, run this command in another:

 $ ​​node​​ ​​zmq-filer-req.js​​ ​​target.txt
 Sending a request for target.txt
 Received response: { content: '', timestamp: 1458898367933, pid: 24815 }

Success! The REP endpoint received the request, processed it, and sent back a response.

Trading Synchronicity for Scale

There is a catch to using ØMQ REP/REQ socket pairs with Node. Each endpoint of the application operates on only one request or one response at a time. There is no parallelism.

We can see this in action by making a small change to the requester program. Open the zmq-filer-req.js file from last section. Find the code that sends the request and wrap it in a for loop like this:

 for​ (​let​ i = 1; i <= 5; i++) {
  console.log(​`Sending request ​${i}​ for ​${filename}​`​);
  requester.send(JSON.stringify({ path: filename }));
 }

Save this file as zmq-filer-req-loop.js. With the responder still running, invoke the new script:

 $ ​​node​​ ​​zmq-filer-req-loop.js​​ ​​target.txt
 Sending request 1 for target.txt
 Sending request 2 for target.txt
 Received response: { content: '', timestamp: 1458902785998, pid: 24674 }
 Sending request 3 for target.txt
 Sending request 4 for target.txt
 Sending request 5 for target.txt
 Received response: { content: '', timestamp: 1458902786010, pid: 24674 }
 Received response: { content: '', timestamp: 1458902786011, pid: 24674 }
 Received response: { content: '', timestamp: 1458902786011, pid: 24674 }
 Received response: { content: '', timestamp: 1458902786012, pid: 24674 }

We see that the requests were queued in the loop, and then we received the responses. The sending and receiving lines may be interleaved, depending on how quickly the responses become available.

This output shouldn’t be too surprising, but let’s take a look at the responder window:

 $ ​​node​​ ​​zmq-filer-rep.js
 Listening for zmq requesters...
 Received request to get: target.txt
 Sending response content.
 Received request to get: target.txt
 Sending response content.
 Received request to get: target.txt
 Sending response content.
 Received request to get: target.txt
 Sending response content.
 Received request to get: target.txt
 Sending response content.

Notice that the responder program sent a response to each request before even becoming aware of the next queued request. This means Node.js’s event loop was left spinning while the fs.readFile for each request was being processed.

For this reason, a simple REQ/REP pair is probably not going to suit your high-performance Node.js needs. Next we’ll construct a cluster of Node.js processes using more advanced ØMQ socket types to scale up our throughput.