Pro Node.js for Developers - Springer

4 downloads 91 Views 137KB Size Report
Pro Node.js for Developers. Copyright © 2013 by ... This book is also dedicated to the entire Node.js community. ... Chapter 3: The Node Programming Model. □.
Pro Node.js for Developers

Colin J. Ihrig

Pro Node.js for Developers Copyright © 2013 by Colin J. Ihrig This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-5860-5 ISBN-13 (electronic): 978-1-4302-5861-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Andy Olsen Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss, Tom Welsh Coordinating Editor: Mark Powers Copy Editors: Thomas McCarthy and Kezia Endsley Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com/9781430258605. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

This book is dedicated to my son, CJ. I love you so much! This book is also dedicated to the entire Node.js community.

  

Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Getting Started�������������������������������������������������������������������������������������������������1 ■■Chapter 2: The Node Module System���������������������������������������������������������������������������������9 ■■Chapter 3: The Node Programming Model�����������������������������������������������������������������������29 ■■Chapter 4: Events and Timers������������������������������������������������������������������������������������������45 ■■Chapter 5: The Command Line Interface�������������������������������������������������������������������������59 ■■Chapter 6: The File System����������������������������������������������������������������������������������������������77 ■■Chapter 7: Streams����������������������������������������������������������������������������������������������������������95 ■■Chapter 8: Binary Data��������������������������������������������������������������������������������������������������109 ■■Chapter 9: Executing Code��������������������������������������������������������������������������������������������129 ■■Chapter 10: Network Programming�������������������������������������������������������������������������������147 ■■Chapter 11: HTTP�����������������������������������������������������������������������������������������������������������167 ■■Chapter 12: The Express Framework����������������������������������������������������������������������������189

v

■ Contents at a Glance

■■Chapter 13: The Real-Time Web������������������������������������������������������������������������������������205 ■■Chapter 14: Databases��������������������������������������������������������������������������������������������������217 ■■Chapter 15: Logging, Debugging, and Testing���������������������������������������������������������������233 ■■Chapter 16: Application Scaling������������������������������������������������������������������������������������249 ■■Appendix A: JavaScript Object Notation������������������������������������������������������������������������263 Index���������������������������������������������������������������������������������������������������������������������������������271

vi

Contents About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Getting Started�������������������������������������������������������������������������������������������������1 The Node Execution Model������������������������������������������������������������������������������������������������������������1 Installing Node������������������������������������������������������������������������������������������������������������������������������2 Installing via Package Managers��������������������������������������������������������������������������������������������������������������������������� 3 Building from Source��������������������������������������������������������������������������������������������������������������������������������������������� 4 Final Installation Steps������������������������������������������������������������������������������������������������������������������������������������������ 4

The Read-Eval-Print-Loop�������������������������������������������������������������������������������������������������������������5 REPL Features������������������������������������������������������������������������������������������������������������������������������������������������������� 5 REPL Commands��������������������������������������������������������������������������������������������������������������������������������������������������� 6

Executing Node Programs�������������������������������������������������������������������������������������������������������������8 Summary���������������������������������������������������������������������������������������������������������������������������������������8 ■■Chapter 2: The Node Module System���������������������������������������������������������������������������������9 Installing Packages�����������������������������������������������������������������������������������������������������������������������9 Installing from URLs�������������������������������������������������������������������������������������������������������������������������������������������� 10 Package Locations����������������������������������������������������������������������������������������������������������������������������������������������� 11 Global Packages�������������������������������������������������������������������������������������������������������������������������������������������������� 11 Linking Packages������������������������������������������������������������������������������������������������������������������������������������������������ 12 Unlinking Packages��������������������������������������������������������������������������������������������������������������������������������������������� 12

vii

■ Contents

Updating Packages���������������������������������������������������������������������������������������������������������������������������������������������� 13 Uninstalling Packages����������������������������������������������������������������������������������������������������������������������������������������� 13

The require( ) Function����������������������������������������������������������������������������������������������������������������13 Core Modules������������������������������������������������������������������������������������������������������������������������������������������������������� 13 File Modules�������������������������������������������������������������������������������������������������������������������������������������������������������� 14 File Extension Processing������������������������������������������������������������������������������������������������������������������������������������ 15 Resolving a Module Location������������������������������������������������������������������������������������������������������������������������������� 15 Module Caching��������������������������������������������������������������������������������������������������������������������������������������������������� 15

The package.json File������������������������������������������������������������������������������������������������������������������16 Description and Keywords����������������������������������������������������������������������������������������������������������������������������������� 17 Author and Contributors�������������������������������������������������������������������������������������������������������������������������������������� 17 The Main Entry Point������������������������������������������������������������������������������������������������������������������������������������������� 18 The preferGlobal Setting�������������������������������������������������������������������������������������������������������������������������������������� 18 Dependencies������������������������������������������������������������������������������������������������������������������������������������������������������ 18 Developmental Dependencies����������������������������������������������������������������������������������������������������������������������������� 19 Optional Dependencies���������������������������������������������������������������������������������������������������������������������������������������� 19 Engines���������������������������������������������������������������������������������������������������������������������������������������������������������������� 19 Scripts����������������������������������������������������������������������������������������������������������������������������������������������������������������� 20 Additional Fields�������������������������������������������������������������������������������������������������������������������������������������������������� 21 Generating a package.json File��������������������������������������������������������������������������������������������������������������������������� 21

A Complete Example�������������������������������������������������������������������������������������������������������������������23 Module Authoring������������������������������������������������������������������������������������������������������������������������24 The module Object����������������������������������������������������������������������������������������������������������������������������������������������� 25 Publishing to npm������������������������������������������������������������������������������������������������������������������������������������������������ 26

Summary�������������������������������������������������������������������������������������������������������������������������������������27 ■■Chapter 3: The Node Programming Model�����������������������������������������������������������������������29 Asynchronous Programming�������������������������������������������������������������������������������������������������������30 Callback Hell�������������������������������������������������������������������������������������������������������������������������������������������������������� 31

Exception Handling����������������������������������������������������������������������������������������������������������������������32 Domains��������������������������������������������������������������������������������������������������������������������������������������������������������������� 33

viii

■ Contents

The async Module�����������������������������������������������������������������������������������������������������������������������36 Executing in Series���������������������������������������������������������������������������������������������������������������������������������������������� 36 Executing in Parallel�������������������������������������������������������������������������������������������������������������������������������������������� 39 The Waterfall Model��������������������������������������������������������������������������������������������������������������������������������������������� 41 The Queue Model������������������������������������������������������������������������������������������������������������������������������������������������� 41 Repeating Methods���������������������������������������������������������������������������������������������������������������������������������������������� 43 Additional async Functionality����������������������������������������������������������������������������������������������������������������������������� 44

Summary�������������������������������������������������������������������������������������������������������������������������������������44 ■■Chapter 4: Events and Timers������������������������������������������������������������������������������������������45 Event Emitters�����������������������������������������������������������������������������������������������������������������������������45 Listening for Events��������������������������������������������������������������������������������������������������������������������������������������������� 46 Inspecting Event Listeners���������������������������������������������������������������������������������������������������������������������������������� 47 The newListener Event���������������������������������������������������������������������������������������������������������������������������������������� 48 Removing Event Listeners����������������������������������������������������������������������������������������������������������������������������������� 48 Detecting Potential Memory Leaks���������������������������������������������������������������������������������������������������������������������� 49 Inheriting from Event Emitters����������������������������������������������������������������������������������������������������������������������������� 50 Using Events to Avoid Callback Hell��������������������������������������������������������������������������������������������������������������������� 51

Timers and Scheduling����������������������������������������������������������������������������������������������������������������52 Intervals��������������������������������������������������������������������������������������������������������������������������������������������������������������� 53 The ref( ) and unref( ) Methods����������������������������������������������������������������������������������������������������������������������������� 53 Immediates���������������������������������������������������������������������������������������������������������������������������������������������������������� 54 Scheduling with process.nextTick( )�������������������������������������������������������������������������������������������������������������������� 55

Summary�������������������������������������������������������������������������������������������������������������������������������������58 ■■Chapter 5: The Command Line Interface�������������������������������������������������������������������������59 Command Line Arguments����������������������������������������������������������������������������������������������������������59 Parsing Argument Values������������������������������������������������������������������������������������������������������������������������������������� 60 Command Line Arguments in commander���������������������������������������������������������������������������������������������������������� 61

The Standard Streams�����������������������������������������������������������������������������������������������������������������63 Standard Input����������������������������������������������������������������������������������������������������������������������������������������������������� 63 Standard Output�������������������������������������������������������������������������������������������������������������������������������������������������� 66

ix

■ Contents

Standard Error����������������������������������������������������������������������������������������������������������������������������������������������������� 71 The TTY Interface������������������������������������������������������������������������������������������������������������������������������������������������� 73

Signal Events�������������������������������������������������������������������������������������������������������������������������������75 User Environment Variables��������������������������������������������������������������������������������������������������������75 Summary�������������������������������������������������������������������������������������������������������������������������������������76 ■■Chapter 6: The File System����������������������������������������������������������������������������������������������77 Relevant Paths����������������������������������������������������������������������������������������������������������������������������77 The Current Working Directory���������������������������������������������������������������������������������������������������������������������������� 78 Locating the node Executable����������������������������������������������������������������������������������������������������������������������������� 79

The path Module�������������������������������������������������������������������������������������������������������������������������79 Cross-Platform Differences��������������������������������������������������������������������������������������������������������������������������������� 79 Extracting Path Components������������������������������������������������������������������������������������������������������������������������������� 80 Path Normalization���������������������������������������������������������������������������������������������������������������������������������������������� 81 Resolving a Relative Path Between Directories��������������������������������������������������������������������������������������������������� 82

The fs Module������������������������������������������������������������������������������������������������������������������������������82 Determining if a File Exists���������������������������������������������������������������������������������������������������������������������������������� 82 Retrieving File Statistics�������������������������������������������������������������������������������������������������������������������������������������� 83 Opening Files������������������������������������������������������������������������������������������������������������������������������������������������������� 85 Reading Data from Files�������������������������������������������������������������������������������������������������������������������������������������� 86 Writing Data to Files�������������������������������������������������������������������������������������������������������������������������������������������� 87 Closing Files�������������������������������������������������������������������������������������������������������������������������������������������������������� 89 Renaming Files���������������������������������������������������������������������������������������������������������������������������������������������������� 89 Deleting Files������������������������������������������������������������������������������������������������������������������������������������������������������� 90 Creating Directories��������������������������������������������������������������������������������������������������������������������������������������������� 90 Reading the Contents of a Directory�������������������������������������������������������������������������������������������������������������������� 91 Removing Directories������������������������������������������������������������������������������������������������������������������������������������������ 91 Watching Files����������������������������������������������������������������������������������������������������������������������������������������������������� 93

Summary�������������������������������������������������������������������������������������������������������������������������������������94

x

■ Contents

■■Chapter 7: Streams����������������������������������������������������������������������������������������������������������95 What Are Streams? ���������������������������������������������������������������������������������������������������������������������95 Working with Streams�����������������������������������������������������������������������������������������������������������������95 Readable Streams�����������������������������������������������������������������������������������������������������������������������95 data Events���������������������������������������������������������������������������������������������������������������������������������������������������������� 96 The end Event������������������������������������������������������������������������������������������������������������������������������������������������������ 96 The close Event��������������������������������������������������������������������������������������������������������������������������������������������������� 97 error Events��������������������������������������������������������������������������������������������������������������������������������������������������������� 97 Controlling Readable Streams����������������������������������������������������������������������������������������������������������������������������� 97

Writable Streams�������������������������������������������������������������������������������������������������������������������������97 The write( ) Method���������������������������������������������������������������������������������������������������������������������������������������������� 98 The end( ) Method������������������������������������������������������������������������������������������������������������������������������������������������ 98 The drain Event���������������������������������������������������������������������������������������������������������������������������������������������������� 98 The finish Event��������������������������������������������������������������������������������������������������������������������������������������������������� 98 The close and error Events���������������������������������������������������������������������������������������������������������������������������������� 99 An Example of a Writable Stream������������������������������������������������������������������������������������������������������������������������ 99

Pipes�������������������������������������������������������������������������������������������������������������������������������������������99 The pipe( ) Method��������������������������������������������������������������������������������������������������������������������������������������������� 100 Back to the Writable Stream Example��������������������������������������������������������������������������������������������������������������� 101

File Streams������������������������������������������������������������������������������������������������������������������������������102 createReadStream( )������������������������������������������������������������������������������������������������������������������������������������������ 102 createWriteStream( )������������������������������������������������������������������������������������������������������������������������������������������ 105

Compression Using the zlib Module������������������������������������������������������������������������������������������106 Deflate/Inflate and DeflateRaw/InflateRaw������������������������������������������������������������������������������������������������������� 107 Convenience Methods��������������������������������������������������������������������������������������������������������������������������������������� 107

Summary�����������������������������������������������������������������������������������������������������������������������������������108

xi

■ Contents

■■Chapter 8: Binary Data��������������������������������������������������������������������������������������������������109 An Overview of Binary Data������������������������������������������������������������������������������������������������������109 Endianness�������������������������������������������������������������������������������������������������������������������������������������������������������� 110

The Typed Array Specification���������������������������������������������������������������������������������������������������111 ArrayBuffers������������������������������������������������������������������������������������������������������������������������������������������������������ 112 ArrayBuffer Views���������������������������������������������������������������������������������������������������������������������������������������������� 114

Node Buffers�����������������������������������������������������������������������������������������������������������������������������121 The Buffer Constructor�������������������������������������������������������������������������������������������������������������������������������������� 121 Stringification Methods������������������������������������������������������������������������������������������������������������������������������������� 123 Buffer.isEncoding( )�������������������������������������������������������������������������������������������������������������������������������������������� 123 Buffer.isBuffer( )������������������������������������������������������������������������������������������������������������������������������������������������� 124 Buffer.byteLength( ) and length�������������������������������������������������������������������������������������������������������������������������� 124 fill( )�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 124 write( )���������������������������������������������������������������������������������������������������������������������������������������������������������������� 124 Writing Numeric Data���������������������������������������������������������������������������������������������������������������������������������������� 125 Reading Numeric Data��������������������������������������������������������������������������������������������������������������������������������������� 126 slice( )���������������������������������������������������������������������������������������������������������������������������������������������������������������� 127 copy( )���������������������������������������������������������������������������������������������������������������������������������������������������������������� 127 Buffer.concat( )��������������������������������������������������������������������������������������������������������������������������������������������������� 127 Typed Array Compatibility���������������������������������������������������������������������������������������������������������������������������������� 128

Summary�����������������������������������������������������������������������������������������������������������������������������������128 ■■Chapter 9: Executing Code��������������������������������������������������������������������������������������������129 The child_process Module��������������������������������������������������������������������������������������������������������129 exec( )���������������������������������������������������������������������������������������������������������������������������������������������������������������� 129 execFile( )����������������������������������������������������������������������������������������������������������������������������������������������������������� 131 spawn( )������������������������������������������������������������������������������������������������������������������������������������������������������������� 132 The ChildProcess Class������������������������������������������������������������������������������������������������������������������������������������� 134 The error Event�������������������������������������������������������������������������������������������������������������������������������������������������� 135 The exit Event���������������������������������������������������������������������������������������������������������������������������������������������������� 135

xii

■ Contents

The close Event������������������������������������������������������������������������������������������������������������������������������������������������� 135 The pid Property������������������������������������������������������������������������������������������������������������������������������������������������ 136 kill( )������������������������������������������������������������������������������������������������������������������������������������������������������������������� 136 fork( )����������������������������������������������������������������������������������������������������������������������������������������������������������������� 136 send( )���������������������������������������������������������������������������������������������������������������������������������������������������������������� 137 disconnect( )������������������������������������������������������������������������������������������������������������������������������������������������������ 139 The vm Module�������������������������������������������������������������������������������������������������������������������������������������������������� 139 runInThisContext( )��������������������������������������������������������������������������������������������������������������������������������������������� 140 runInNewContext( )�������������������������������������������������������������������������������������������������������������������������������������������� 141 runInContext( )��������������������������������������������������������������������������������������������������������������������������������������������������� 143 createScript( )���������������������������������������������������������������������������������������������������������������������������������������������������� 144

Summary�����������������������������������������������������������������������������������������������������������������������������������145 ■■Chapter 10: Network Programming�������������������������������������������������������������������������������147 Sockets��������������������������������������������������������������������������������������������������������������������������������������147 Client-Server Programming������������������������������������������������������������������������������������������������������148 Transmission Control Protocol���������������������������������������������������������������������������������������������������149 Creating a TCP Server���������������������������������������������������������������������������������������������������������������������������������������� 150 Listening for Connections���������������������������������������������������������������������������������������������������������������������������������� 150 Handling Connections���������������������������������������������������������������������������������������������������������������������������������������� 153 Shutting Down the Server��������������������������������������������������������������������������������������������������������������������������������� 154 ref( ) and unref( )������������������������������������������������������������������������������������������������������������������������������������������������ 154 error Events������������������������������������������������������������������������������������������������������������������������������������������������������� 155 Creating a TCP Client����������������������������������������������������������������������������������������������������������������������������������������� 155 The net.Socket Class����������������������������������������������������������������������������������������������������������������������������������������� 157 Sockets, Servers, and Child Processes�������������������������������������������������������������������������������������������������������������� 159

User Datagram Protocol������������������������������������������������������������������������������������������������������������159 Creating UDP Sockets���������������������������������������������������������������������������������������������������������������������������������������� 160 Binding to a Port������������������������������������������������������������������������������������������������������������������������������������������������ 160 Receiving Data��������������������������������������������������������������������������������������������������������������������������������������������������� 161 Sending Data����������������������������������������������������������������������������������������������������������������������������������������������������� 161

xiii

■ Contents

Domain Name System���������������������������������������������������������������������������������������������������������������162 Performing Lookups������������������������������������������������������������������������������������������������������������������������������������������ 162 Reverse Lookups����������������������������������������������������������������������������������������������������������������������������������������������� 164 Detecting Valid IP Addresses����������������������������������������������������������������������������������������������������������������������������� 164

Summary�����������������������������������������������������������������������������������������������������������������������������������165 ■■Chapter 11: HTTP�����������������������������������������������������������������������������������������������������������167 A Basic Server���������������������������������������������������������������������������������������������������������������������������167 Anatomy of an HTTP Request����������������������������������������������������������������������������������������������������168 Request Methods����������������������������������������������������������������������������������������������������������������������������������������������� 168 Request Headers����������������������������������������������������������������������������������������������������������������������������������������������� 170 Response Codes������������������������������������������������������������������������������������������������������������������������������������������������ 170 Response Headers��������������������������������������������������������������������������������������������������������������������������������������������� 172

Working with Cookies����������������������������������������������������������������������������������������������������������������175 Middleware�������������������������������������������������������������������������������������������������������������������������������176 Connect������������������������������������������������������������������������������������������������������������������������������������������������������������� 177

Issuing HTTP Requests��������������������������������������������������������������������������������������������������������������178 Form Data���������������������������������������������������������������������������������������������������������������������������������������������������������� 181 The request Module������������������������������������������������������������������������������������������������������������������������������������������� 183

HTTPS����������������������������������������������������������������������������������������������������������������������������������������186 Summary�����������������������������������������������������������������������������������������������������������������������������������188 ■■Chapter 12: The Express Framework����������������������������������������������������������������������������189 Express Routes��������������������������������������������������������������������������������������������������������������������������189 Route Parameters���������������������������������������������������������������������������������������������������������������������������������������������� 191

Creating an Express Application������������������������������������������������������������������������������������������������192 Examining the Skeleton App������������������������������������������������������������������������������������������������������������������������������ 193

Templating���������������������������������������������������������������������������������������������������������������������������������196 express-validator����������������������������������������������������������������������������������������������������������������������198 REST������������������������������������������������������������������������������������������������������������������������������������������199 An Example RESTful API������������������������������������������������������������������������������������������������������������������������������������ 199

Summary�����������������������������������������������������������������������������������������������������������������������������������204 xiv

■ Contents

■■Chapter 13: The Real-Time Web������������������������������������������������������������������������������������205 The WebSockets API������������������������������������������������������������������������������������������������������������������206 Opening a WebSocket���������������������������������������������������������������������������������������������������������������������������������������� 206 Closing WebSockets������������������������������������������������������������������������������������������������������������������������������������������ 206 Checking a WebSocket’s State�������������������������������������������������������������������������������������������������������������������������� 207 The open Event�������������������������������������������������������������������������������������������������������������������������������������������������� 208 The message Event������������������������������������������������������������������������������������������������������������������������������������������� 208 The close Event������������������������������������������������������������������������������������������������������������������������������������������������� 209 The error Event�������������������������������������������������������������������������������������������������������������������������������������������������� 209 Sending Data����������������������������������������������������������������������������������������������������������������������������������������������������� 209

WebSockets in Node�����������������������������������������������������������������������������������������������������������������209 A WebSocket Client�������������������������������������������������������������������������������������������������������������������������������������������� 210 A HTML Client���������������������������������������������������������������������������������������������������������������������������������������������������� 211 Examining the WebSocket Connection�������������������������������������������������������������������������������������������������������������� 212

Socket.IO�����������������������������������������������������������������������������������������������������������������������������������213 Creating a Socket.IO Server������������������������������������������������������������������������������������������������������������������������������� 213 Creating a Socket.IO Client�������������������������������������������������������������������������������������������������������������������������������� 214 Socket.IO and Express��������������������������������������������������������������������������������������������������������������������������������������� 215

Summary�����������������������������������������������������������������������������������������������������������������������������������215 ■■Chapter 14: Databases��������������������������������������������������������������������������������������������������217 Relational Databases�����������������������������������������������������������������������������������������������������������������217 MySQL���������������������������������������������������������������������������������������������������������������������������������������219 Connecting to MySQL���������������������������������������������������������������������������������������������������������������������������������������� 219 Connection Pooling�������������������������������������������������������������������������������������������������������������������������������������������� 221 Closing a Connection����������������������������������������������������������������������������������������������������������������������������������������� 222 Executing Queries���������������������������������������������������������������������������������������������������������������������������������������������� 223

NoSQL Databases����������������������������������������������������������������������������������������������������������������������225 MongoDB�����������������������������������������������������������������������������������������������������������������������������������225 Connecting to MongoDB������������������������������������������������������������������������������������������������������������������������������������ 225 Schemas������������������������������������������������������������������������������������������������������������������������������������������������������������ 226

xv

■ Contents

Models��������������������������������������������������������������������������������������������������������������������������������������������������������������� 227 Inserting Data���������������������������������������������������������������������������������������������������������������������������������������������������� 228 Querying Data���������������������������������������������������������������������������������������������������������������������������������������������������� 229 Query Builder Methods�������������������������������������������������������������������������������������������������������������������������������������� 230 Updating Data���������������������������������������������������������������������������������������������������������������������������������������������������� 231 Deleting Data����������������������������������������������������������������������������������������������������������������������������������������������������� 232

Summary�����������������������������������������������������������������������������������������������������������������������������������232 ■■Chapter 15: Logging, Debugging, and Testing���������������������������������������������������������������233 Logging�������������������������������������������������������������������������������������������������������������������������������������233 The winston Module������������������������������������������������������������������������������������������������������������������������������������������ 234

Debugging���������������������������������������������������������������������������������������������������������������������������������237 The node-inspector Module������������������������������������������������������������������������������������������������������������������������������� 238

Testing���������������������������������������������������������������������������������������������������������������������������������������240 The assert Module��������������������������������������������������������������������������������������������������������������������������������������������� 240 The Mocha Testing Framework�������������������������������������������������������������������������������������������������������������������������� 244

Summary�����������������������������������������������������������������������������������������������������������������������������������248 ■■Chapter 16: Application Scaling������������������������������������������������������������������������������������249 The cluster Module��������������������������������������������������������������������������������������������������������������������249 The fork( ) Method���������������������������������������������������������������������������������������������������������������������������������������������� 250 The disconnect( ) Method���������������������������������������������������������������������������������������������������������������������������������� 252 The workers Object������������������������������������������������������������������������������������������������������������������������������������������� 253 The Worker Class����������������������������������������������������������������������������������������������������������������������������������������������� 254

Scaling Across Machines����������������������������������������������������������������������������������������������������������255 http-proxy���������������������������������������������������������������������������������������������������������������������������������������������������������� 255 nginx������������������������������������������������������������������������������������������������������������������������������������������������������������������ 256

Scaling in the Cloud������������������������������������������������������������������������������������������������������������������260 Nodejitsu����������������������������������������������������������������������������������������������������������������������������������������������������������� 260 Heroku��������������������������������������������������������������������������������������������������������������������������������������������������������������� 261

Summary�����������������������������������������������������������������������������������������������������������������������������������262

xvi

■ Contents

■■Appendix A: JavaScript Object Notation������������������������������������������������������������������������263 Supported Data Types���������������������������������������������������������������������������������������������������������������263 Numbers������������������������������������������������������������������������������������������������������������������������������������������������������������ 263 Strings��������������������������������������������������������������������������������������������������������������������������������������������������������������� 264 Booleans������������������������������������������������������������������������������������������������������������������������������������������������������������ 264 Arrays���������������������������������������������������������������������������������������������������������������������������������������������������������������� 265 Objects��������������������������������������������������������������������������������������������������������������������������������������������������������������� 265 null��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 265 Unsupported Data Types������������������������������������������������������������������������������������������������������������������������������������ 265

Functions for Working with JSON����������������������������������������������������������������������������������������������266 JSON.stringify( )������������������������������������������������������������������������������������������������������������������������������������������������� 266 JSON.parse( )����������������������������������������������������������������������������������������������������������������������������������������������������� 269

Summary�����������������������������������������������������������������������������������������������������������������������������������270 Index���������������������������������������������������������������������������������������������������������������������������������271

xvii

  

About the Author Colin Ihrig has been experimenting with JavaScript for fun and profit for over 15 years. He is currently a full-time Node.js engineer, as well as a JavaScript writer and evangelist in his spare time. Colin received his Bachelor of Science in Engineering and Master of Science in Computer Engineering from the University of Pittsburgh in 2005 and 2008, respectively. Colin can be reached via his personal web page at http://www.cjihrig.com.

xix

  

About the Technical Reviewer Andy Olsen is a freelance consultant/trainer based in the UK, and has been working in distributed systems for 20 years. Andy started working in C in the mid 1980s, but it might as well have been the mid 1880s, it seems so long ago. Andy migrated into C++, Java, and .NET as times and fashions changed, and is currently kept (too?) busy in web-based systems, both client-side and server-side. Andy lives by the seaside in Swansea and enjoys running, coffee shops, and watching the Swans.

xxi

  

Acknowledgments I would like to thank everyone who helped make this book possible. Special thanks to Mark Powers and Ewan Buckingham of the Apress editorial team. I would also like to thank the technical reviewer, Andy Olsen, for his valuable feedback. Of course, many thanks go out to my friends and family.

xxiii

Introduction Since its creation in 2009, Node.js has grown into a powerful and increasingly popular asynchronous development framework, used for creating highly scalable JavaScript applications. Respected companies such as Dow Jones, LinkedIn, and Walmart are among the many organizations to have seen Node’s potential and adopted it into their businesses. Pro Node.js for Developers provides a comprehensive guide to this exciting young technology. You will be introduced to Node at a high level before diving deeply into the key concepts and APIs that underpin its operation. Building upon your existing JavaScript skills, you’ll be shown how to use Node.js to build both web- and network-based applications, to deal with various data sources, capture and generate events, spawn and control child processes, and much more. Once you’ve mastered these skills, you’ll learn more advanced software engineering skills that will give your code a professional edge. You’ll learn how to create easily reusable code modules, debug and test your applications quickly and effectively, and scale your code from a single thread to the cloud as demand for your application increases.

xxv