ebook img

Learning Node Moving to the Server-Side (Early Release) PDF

280 Pages·2016·11.372 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learning Node Moving to the Server-Side (Early Release)

Learning Node Second Edition Author Name Learning Node by Shelley Powers Copyright © 2016 Shelly Powers. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebasto- pol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotion- al use. Online editions are also available for most titles ( http://safaribookson- line.com ). For more information, contact our corporate/institutional sales de- partment: 800-998-9938 or [email protected] . • Editor: Meg Foley • Production Editor: FILL IN PRODUCTION EDITOR • Copyeditor: FILL IN COPYEDITOR • Proofreader: FILL IN PROOFREADER • Indexer: FILL IN INDEXER • Interior Designer: David Futato • Cover Designer: Karen Montgomery • Illustrator: Rebecca Demarest • January -4712: Second Edition Revision History for the Second Edition • 2016-03-15: First Early Release See http://oreilly.com/catalog/errata.csp?isbn=9781491943052 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Node, the cover image, and related trade dress are trademarks of O’Reilly Me- dia, Inc. While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual prop- erty rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-94305-2 [FILL IN] Table of Contents Preface ix CHAPTER 1: The Node Environment 15 Installing Node 15 Saying Hello to the World with Node 17 A Basic Hello World Application 17 Hello World, Tweaked 21 Node Command-Line Options 24 Node Hosting Environments 25 Hosting Node on Your Server, VPS, or Managed Host 25 Cloud Hosting 26 The Node LTS and Upgrading Node 27 Node’s New Semantic Versioning 27 Upgrading Node 29 Node, V8, and ES6 30 Advanced: Node C/C++ Add-ons 31 CHAPTER 2: Node Building Blocks: the Global Objects, Events, and Node’s Asynchronous Nature 33 The global and process objects 33 The global Object 34 The process Object 35 Buffers, Typed Arrays, and Strings 39 iii Table of Contents Buffer, JSON, StringDecoder, and UTF-8 Strings 41 Buffer Manipulation 43 Node’s Callback and Asynchronous Event Handling 46 The Event Queue (Loop) 47 Creating an Asynchronous Callback Function 50 EventEmitter 53 The Node Event Loop and Timers 57 Nested Callbacks and Exception Handling 60 CHAPTER 3: Basics of Node Modules and Npm 71 An Overview of the Node Module System 71 How Node Finds and Loads a Module 72 Sandboxing and the VM Module 76 An In-Depth Exploration of NPM 80 Creating and Publishing Your Own Node Module 86 Creating a Module 86 Packaging an Entire Directory 87 Preparing Your Module for Publication 88 Publishing the Module 92 Discovering Node Modules and Three Must-Have Modules 93 Better Callback Management with async 94 Command-Line Magic with Commander 100 The Ubiquitous Underscore 102 CHAPTER 4: Interactive Node with REPL and More on the Console 105 REPL: First Looks and Undefined Expressions 105 Benefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood 107 Multiline and More Complex JavaScript 108 REPL Commands 113 REPL and rlwrap 114 Custom REPL 115 Stuff Happens—Save Often 119 The Necessity of Console 120 Console Message Types, Console Class, and Blocking 120 iv Table of Contents Formatting the Message, with Help From util.format() and util.inspect() 122 Providing Richer Feedback with console and a Timer 126 CHAPTER 5: Node and the Web 129 The HTTP Module: Server and Client 129 What’s Involved in Creating a Static Web Server 134 Using Apache to Proxy a Node Application 143 Parsing the Query with Query String 145 DNS Resolution 146 CHAPTER 6: Node and the Local System 149 Exploring the Operating System 149 Streams and Pipes 151 A Formal Introduction to the File System (fs) 153 The fs.Stats Class 153 The File System Watcher 155 File Read and Write 156 Directory Access and Maintenance 158 File Streams 158 Resource Access with Path 161 Creating a Command-Line Utility 163 Compression/Decompression with ZLib 165 Pipes and ReadLine 169 CHAPTER 7: Networking, Sockets, and Security 173 Servers, Streams, and Sockets 173 Sockets and Streams 173 TCP Sockets and Servers 174 UDP/Datagram Socket 180 Guards at the Gate 181 Setting Up TLS/SSL 181 Working with HTTPS 183 The Crypto Module 186 CHAPTER 8: Child Processes 191 v Table of Contents child_process.spawn 191 child_process.exec and child_process.execFile 194 child_process.fork 196 Running a Child Process Application in Windows 197 CHAPTER 9: Node and ES6 199 Strict Mode 199 let and const 201 Arrow Functions 203 Classes 205 Promises with Bluebird 208 CHAPTER 10: Full-stack Node Development 213 The Express Application Framework 213 MongoDB and Redis Database Systems 219 MongoDB 219 Redis Key/Value Store 223 AngularJS and Other Full-Stack Frameworks 231 CHAPTER 11: Node in Development and Production 239 Debugging Node Applications 239 The Node Debugger 239 Node Inspector 245 Unit Testing 248 Unit Testing with Assert 248 Unit Testing with Nodeunit 252 Other Testing Frameworks 254 Keeping Node Up and Running 257 Benchmark and Load Testing with Apache Bench 260 CHAPTER 12: Node in New Environments 263 Samsung IoT and GPIO 263 Windows with Charka Node 265 Node for Microcontrollers and Microcomputers 266 Fritzing 267 vi Table of Contents Node and Adruino 270 Node and Raspberry Pi 2 275 vii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.