Expert techniques for building fast servers and scalable, real-time network applications with minimal effort **Overview** * Master the latest techniques for building real-time, big data applications, integrating Facebook, Twitter, and other network services * Tame asynchronous programming, the event loop, and parallel data processing * Use the Express and Path frameworks to speed up development and deliver scalable, higher quality software more quickly **In Detail** Node.js is a modern development stack focused on providing an easy way to build scalable network software. Backed by a growing number of large companies and a rapidly increasing developer base, Node is revolutionizing the way that software is being built today. Powered by Google’s V8 engine and built out of C++ modules, this is a JavaScript environment for the enterprise. Mastering Node.js will take the reader deep into this exciting development environment. Beginning with a comprehensive breakdown of its innovative non-blocking evented design, Node’s structure is explained in detail, laying out how its blazingly fast I/O performance simplifies the creation of fast servers, scalable architectures, and responsive web applications. Mastering Node.js takes you through a concise yet thorough tour of Node's innovative evented non-blocking design, showing you how to build professional applications with the help of detailed examples. Learn how to integrate your applications with Facebook and Twitter, Amazon and Google, creating social apps and programs reaching thousands of collaborators on the cloud. See how the Express and Path frameworks make the creation of professional web applications painless. Set up one, two, or an entire server cluster with just a few lines of code, ready to scale as soon as you’re ready to launch. Move data seamlessly between databases and file systems, between clients, and across network protocols, using a beautifully designed, consistent, and predictable set of tools. Mastering Node.js contains all of the examples and explanations you’ll need to build applications in a short amount of time and at a low cost, running on a scale and speed that would have been nearly impossible just a few years ago. **What you will learn from this book** * Discover how Node uses Google’s V8 engine to create high-speed JavaScript on the server * Use events to create non-blocking systems with high throughput * Create secure servers across all major network protocols * Manage thousands of concurrent clients without slowing down * Handle data, files, and protocol streams with ease * Leverage Node’s module system to design sane applications that are easy to extend * Optimize and debug server-side JavaScript for increased performance * Learn scaling techniques and parallelize operations across multiple cores **Approach** Packed with practical examples and explanations, Mastering Node.js contains everything you need to take your applications to the next level. **Who this book is written for** If you’ve ever built a web page and would like to build network software using the language and style you already know, Node will make that process simple, and even fun. If you understand what a server is, what a client is, and how to write JavaScript, you are already halfway to mastering Node.js! Cover 1 Copyright 3 Credits 4 About the Author 5 Acknowledgments 6 About the Reviewers 7 www.PacktPub.com 8 Table of Contents 10 Preface 18 Chapter 1: Understanding the Node Environment 24 Extending JavaScript 26 Events 27 Modularity 29 The Network 30 V8 32 Memory and other limits 33 Harmony 35 The process object 36 The Read-Eval-Print Loop and executing a Node program 38 Summary 40 Chapter 2: Understanding Asynchronous Event-Driven Programming 42 Broadcasting events 43 Collaboration 45 Queueing 46 Listening for events 47 Signals 47 Forks 49 File events 51 Deferred execution 52 process.nextTick 53 Timers 55 setTimeout 55 setInterval 56 unref and ref 57 Understanding the event loop 58 Four sources of truth 59 Callbacks and errors 61 Conventions 62 Know your errors 62 Building pyramids 64 Considerations 65 Listening for file changes 66 Summary 70 Chapter 3: Streaming Data Across Nodes and Clients 72 Exploring streams 74 Implementing readable streams 76 Pushing and pulling 78 Writable streams 79 Duplex streams 82 Transforming streams 82 Using PassThrough streams 83 Creating an HTTP server 84 Making HTTP requests 86 Proxying and tunneling 87 HTTPS, TLS(SSL), and securing your server 89 Creating a self-signed certificate for development 89 Installing a real SSL certificate 90 The request object 90 The URL module 91 The Querystring module 93 Working with headers 94 Using cookies 95 Understanding content types 97 Handling favicon requests 98 Handling POST data 99 Creating and streaming images with Node 100 Creating, caching, and sending a PNG representation 101 Summary 104 Chapter 4: Using Node to Access the Filesystem 106 Directories, and iterating over files and folders 107 Types of files 108 File paths 109 File attributes 111 Opening and closing files 112 fs.open(path, flags, [mode], callback) 113 fs.close(fd, callback) 114 File operations 114 fs.rename(oldName, newName, callback) 114 fs.truncate(path, len, callback) 114 fs.ftruncate(fd, len, callback) 114 fs.chown(path, uid, gid, callback) 115 fs.fchown(fd, uid, gid, callback) 115 fs.lchown(path, uid, gid, callback) 115 fs.chmod(path, mode, callback) 115 fs.fchmod(fd, mode, callback) 115 fs.lchmod(path, mode, callback) 116 fs.link(srcPath, dstPath, callback) 116 fs.symlink(srcPath, dstPath, [type], callback) 116 fs.readlink(path, callback) 117 fs.realpath(path, [cache], callback) 117 fs.unlink(path, callback) 118 fs.rmdir(path, callback) 118 fs.mkdir(path, [mode], callback) 118 fs.exists(path, callback) 118 fs.fsync(fd, callback) 118 Synchronicity 119 Moving through directories 120 Reading from a file 122 Reading byte by byte 123 fs.read(fd, buffer, offset, length, position, callback) 123 Fetching an entire file at once 124 fs.readFile(path, [options], callback) 124 Creating a readable stream 124 fs.createReadStream(path, [options]) 125 Reading a file line by line 125 The Readline module 126 Writing to a file 127 Writing byte by byte 127 fs.write(fd, buffer, offset, length, position, callback) 127 Writing large chunks of data 129 fs.writeFile(path, data, [options], callback) 129 fs.appendFile(path, data, [options], callback) 129 Creating a writable stream 130 fs.createWriteStream(path, [options]) 130 Caveats 130 Serving static files 131 Redirecting requests 131 Location 132 Implementing resource caching 133 Handling file uploads 135 Putting it all together 137 Summary 138 Chapter 5: Managing Many Simultaneous Client Connections 140 Understanding concurrency 143 Concurrency is not parallelism 143 Routing requests 144 Understanding routes 146 Using Express to route requests 148 Using Redis for tracking client state 149 Storing user data 151 Handling sessions 152 Cookies and client state 152 A simple poll 153 Centralizing states 155 Authenticating connections 157 Basic authentication 158 Handshaking 160 Summary 163 Further reading 163 Chapter 6: Creating Real-time Applications 164 Introducing AJAX 166 Responding to calls 168 Creating a stock ticker 169 Bidirectional communication with Socket.IO 173 Using the WebSocket API 174 Socket.IO 176 Drawing collaboratively 178 Listening for Server Sent Events 182 Using the EventSource API 183 The EventSource stream protocol 186 Asking questions and getting answers 188 Building a collaborative document editing application 195 Summary 199 Chapter 7: Utilizing Multiple Processes 200 Node's single-threaded model 202 The benefits of single-threaded programming 203 Multithreading is already native and transparent 206 Creating child processes 207 Spawning processes 209 Forking processes 212 Buffering process output 214 Communicating with your child 215 Sending messages to children 216 Parsing a file using multiple processes 217 Using the cluster module 220 Cluster events 222 Worker object properties 222 Worker events 223 Real-time activity updates of multiple worker results 223 Summary 229 Chapter 8: Scaling Your Application 230 When to scale? 231 Network latency 232 Hot CPUs 233 Socket usage 235 Many file descriptors 235 Data creep 235 Tools for monitoring servers 237 Running multiple Node servers 237 Forward and reverse proxies 237 Nginx as a proxy 239 Using HTTP Proxy 242 Message queues – RabbitMQ 244 Types of exchanges 245 Using Node's UDP module 247 UDP multicasting with Node 250 Using Amazon Web Services in your application 253 Authenticating 254 Errors 255 Using S3 to store files 256 Working with buckets 256 Working with objects 257 Using AWS with a Node server 260 Getting and setting data with DynamoDB 261 Searching the database 264 Sending mail via SES 265 Authenticating with Facebook Connect 267 Summary 270 Chapter 9: Testing your Application 272 Why testing is important 273 Unit tests 274 Functional tests 274 Integration tests 275 Native Node testing and debugging tools 276 Writing to the console 276 Formatting console output 278 The Node debugger 280 The assert module 284 Sandboxing 287 Distinguishing between local scope and execution context 288 Using compiled contexts 289 Errors and exceptions 289 The domain module 292 Headless website testing with ZombieJS and Mocha 294 Mocha 295 Headless web testing 296 Using Grunt, Mocha, and PhantomJS to test and deploy projects 298 Working with Grunt 300 Summary 301 Appendix A: Organizing Your Work 302 Loading and using modules 303 Understanding the module object 304 Resolving module paths 305 Using npm 307 Initializing a package file 307 Using scripts 308 Declaring dependencies 309 Publishing packages 310 Globally installing packages and binaries 311 Sharing repositories 312 Appendix B: Introducing the Path Framework 314 Managing state 316 Bridging the client/server divide 317 Sending and receiving 319 Achieving a modular architecture 320 Appendix C: Creating your own C++ Add-ons 324 Hello World 326 Creating a calculator 328 Implementing callbacks 330 Closing thoughts 331 Links and resources 332 Index 334 Using the JavaScript language you already know, this book will show you how to easily build scalable network software with Node.js. You'll learn through practical examples and clear explanations every step of the way. Master the latest techniques for building real-time, big data applications, integrating Facebook, Twitter, and other network services Tame asynchronous programming, the event loop, and parallel data processing Use the Express and Path frameworks to speed up development and deliver scalable, higher quality software more quickly In Detail Node.js is a modern development stack focused on providing an easy way to build scalable network software. Backed by a growing number of large companies and a rapidly increasing developer base, Node is revolutionizing the way that software is being built today. Powered by Google's V8 engine and built out of C++ modules, this is a JavaScript environment for the enterprise. Mastering Node.js will take the reader deep into this exciting development environment. Beginning with a comprehensive breakdown of its innovative non-blocking evented design, Node's structure is explained in detail, laying out how its blazingly fast I/O performance simplifies the creation of fast servers, scalable architectures, and responsive web applications. Mastering Node.js takes you through a concise yet thorough tour of Node's innovative evented non-blocking design, showing you how to build professional applications with the help of detailed examples. Learn how to integrate your applications with Facebook and Twitter, Amazon and Google, creating social apps and programs reaching thousands of collaborators on the cloud. See how the Express and Path frameworks make the creation of professional web applications painless. Set up one, two, or an entire server cluster with just a few lines of code, ready to scale as soon as you're ready to launch. Move data seamlessly between databases and file systems, between clients, and across network protocols, using a beautifully designed, consistent, and predictable set of tools. Mastering Node.js contains all of the examples and explanations you'll need to build applications in a short amount of time and at a low cost, running on a scale and speed that would have been nearly impossible just a few years ago This book contains an extensive set of practical examples and an easy-to-follow approach to creating 3D objects.This book is great for anyone who already knows JavaScript and who wants to start creating 3D graphics that run in any browser. You don't need to know anything about advanced math or WebGL; all that is needed is a general knowledge of JavaScript and HTML. The required materials and examples can be freely downloaded and all tools used in this book are open source. This book contains an extensive set of practical examples and an easy-to-follow approach to creating 3D objects. This book is great for anyone who already knows JavaScript and who wants to start creating 3D graphics that run in any browser. You don't need to know anything about advanced math or WebGL; all that is needed is a general knowledge of JavaScript and HTML. The required materials and examples can be freely downloaded and all tools used in this book are open source