Preface
The principles of asynchronous event-driven programming are perfect for today's web, where efficient, high-concurrency applications are essential for good user experience and a company's bottom line.
The use of Node for tooling and server-side logic with a browser-based, client-side UI leads to a full-stack unilingual experience--everything is JavaScript. This saves developers, architects, project leads, and entire teams the cognitive energy of context-switching between languages, and yields rapid, fluid development cycles.
With a thriving community and success stories and investment from major organizations (such as Netflix, IBM, Capital One, Groupon, RedHat, PayPal, Fidelity, and more), Node.js is relevant to enthusiasts, start-ups, and enterprises alike.
Since the publication of the first edition of Node Cookbook, the technology, community, thinking, and industry around Node.js has taken significant steps forward. On top of that, the first edition was introductory in nature when it was published; Node.js was new, and there were few developers working with it on a daily basis. These days, developer mindshare around Node.js is widespread, allowing for a higher level of assumed basic understanding. To that end, Node Cookbook, Third Edition is an (almost) complete rewrite of Node Cookbook. It covers far more territory than the first and second edition, with dedicated chapters on debugging, performance, microservices, and deployment (all the topics that are either alluded to or not covered in the former editions). Node Cookbook, Third Edition, not only benefits from the enhanced experience and knowledge acquired by the original author since the first edition was written, but also draws on the expertise of coauthors Mathias Buus, Matteo Collina, and Peter Elger, who provide content for some of the high value chapters in this book.
What this book covers
Chapter 1, Debugging Processes, David Mark Clements
Debugging Processes explores some excellent debugging tools for Node, along with techniques and practices to increase visibility and process information as we encounter debugging scenarios.
Chapter 2, Writing Modules, David Mark Clements
Writing Modules teaches how Node's module system works and how to create modules for various scenarios according to industry best practices.
Chapter 3, Coordinating I/O, David Mark Clements
Coordinating I/O explores some core APIs provided by Node, along with a few third-party utilities that allow us to interact with standard I/O, the filesystem, and the network stack.
Chapter 4, Using Streams, Mathias Buus and David Mark Clements
Using Streams explains why streams are so important, how they bring functional programming to an asynchronous world, and how to avoid stream gotchas.
Chapter 5, Wielding Web Protocols, David Mark Clements
Wielding Web Protocols demonstrates how to work with the web at a low level without using web framework. In this chapter, we will explore how to implement clients and servers using various web protocols, including HTTP, SMTP, and WebSockets.
Chapter 6, Persisting to Databases, David Mark Clements
Persisting to Databases takes you through a cross-section of database systems, such as MySQL/MariaDB, Postgres, Redis, MongoDB, and LevelDB, and how to interact with them from within a Node process.
Chapter 7, Working with Web Frameworks, David Mark Clements
Working with Web Frameworks is an exploration creating web applications with three of the most popular web frameworks: Express, Hapi, and Koa. From scaffolding to using middleware/plugins, to working with views, to implementing authentication, this chapter supplies a comprehensive tour through creating a web application with a web framework in Node.js.
Chapter 8, Dealing with Security, David Mark Clements
Dealing with Security covers various attacks that can be made against a system and shows common programmer errors that lead to vulnerable systems, along with some best practices and approaches that help create secure systems.
Chapter 9, Optimizing Performance, Matteo Collina, and David Mark Clements
Optimizing Performance provides an optimization workflow focused on measurement, alteration, and iteration. This chapter demonstrates how to identify bottlenecks, refactor for performance, and develop habits for writing efficient, optimizable JavaScript as an everyday practice.
Chapter 10, Building Microservice Systems, Peter Elger, and David Mark Clements
Building Microservice Systems teaches what microservices are, how they inherently facilitate the scaling up of robust production systems, and what's available in the ecosystem to assist in microservice development.
Chapter 11, Deploying Node.js, Peter Elger, and David Mark Clements
Deploying Node.js demonstrates how to put a multiprocess Node.js system into production, from containerizing processes with Docker to creating a Kubernetes cluster, to building a CI deployment pipeline and deploying associated infrastructure (such as a Docker Registry), to deploying an entire system to a cloud provider.
What you need for this book
The following is a list of the software that is required to run the examples in this book:
- Chapters 1-10: Windows, macOS, or Linux.
- Chapter 11: Linux or macOS (we recommend that Windows users work through Chapter 11, Deploying Node.js, by SSH-ing into a remote Linux machine).
- Node 6 or higher. In cases where the code is specific to Node 8, this is specified. Node can be downloaded from http://nodejs.org.
- Curl 7: Curl can be downloaded from http://curl.haxx.se.
- Chrome Web Browser
Who this book is for
If you have some knowledge of JavaScript and want to build fast, efficient, scalable client- server solutions, then Node Cookbook, Third Edition is excellent introductory material.
If you use Node.js at work, Node Cookbook, Third Edition is definitely for you.
Experienced users of Node can improve their skills and be challenged by philosophical approaches, while beginners can use the practical recipes to acquire foundational understanding by osmosis.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can see line 7 is now highlighted, and there's a sort of tooltip showing us the values of the req
and res
objects on the line above".
A block of code is set as follows:
const express = require('express') const app = express() const past = require('./past') const future = require('./future') app.get('/:age', (req, res) => { res.send(past(req.params.age, 10) + future(req.params.future, 10)) }) app.listen(3000)
Any command-line input or output is written as follows:
$ mkdir app $ cd app $ npm init -y $ npm install --save express $ touch index.js future.js past.js
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Let's open up the Call Stack
bar again and click the second row from the top."
Note
Warnings or important notes appear like this.
Note
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected]
, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can download the code files by following these steps:
- Log in or register to our website using your e-mail address and password.
- Hover the mouse pointer on the
SUPPORT
tab at the top. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box. - Select the book for which you're looking to download the code files.
- Choose from the drop-down menu where you purchased this book from.
- Click on
Code Download
.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR / 7-Zip for Windows
- Zipeg / iZip / UnRarX for Mac
- 7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Node-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form
link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata
section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected]
with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at [email protected]
, and we will do our best to address the problem.