Details

Professional Node.js


Professional Node.js

Building Javascript Based Scalable Software
1. Aufl.

von: Pedro Teixeira

28,99 €

Verlag: Wiley
Format: PDF
Veröffentl.: 01.10.2012
ISBN/EAN: 9781118227541
Sprache: englisch
Anzahl Seiten: 408

DRM-geschütztes eBook, Sie benötigen z.B. Adobe Digital Editions und eine Adobe ID zum Lesen.

Beschreibungen

<b>Learn to build fast and scalable software in JavaScript with Node.js</b> <p>Node.js is a powerful and popular new framework for writing scalable network programs using JavaScript. This no nonsense book begins with an overview of Node.js and then quickly dives into the code, core concepts, and APIs. In-depth coverage pares down the essentials to cover debugging, unit testing, and flow control so that you can start building and testing your own modules right away.</p> <ul> <li>Covers node and asynchronous programming main concepts</li> <li>Addresses the basics: modules, buffers, events, and timers</li> <li>Explores streams, file systems, networking, and automated unit testing</li> <li>Goes beyond the basics, and shares techniques and tools for debugging, unit testing, and flow control</li> </ul> <p>If you already know JavaScript and are curious about the power of Node.js, then this is the ideal book for you.</p>
INTRODUCTION xxvii <p><b>PART I: INTRODUCTION AND SETUP</b></p> <p><b>CHAPTER 1: INSTALLING NODE 3</b></p> <p>Installing Node on Windows 4</p> <p>Installing on Mac OS X 5</p> <p>Installing Node Using the Source Code 6</p> <p>Choosing the Node Version 6</p> <p>Downloading the Node Source Code 6</p> <p>Building Node 7</p> <p>Installing Node 7</p> <p>Running Node 8</p> <p>Setting Up and Using Node Package Manager 8</p> <p>Using NPM to Install, Update, and Uninstall Packages 9</p> <p>Summary 13</p> <p><b>CHAPTER 2: INTRODUCING NODE 15</b></p> <p>Introducing the Event-Driven Programming Style 16</p> <p>How Node and JavaScript Make Writing Asynchronous Applications Easier 17</p> <p>What Are Closures? 18</p> <p>How Closures Help When Programming Asynchronously 19</p> <p>Summary 19</p> <p><b>PART II: NODE CORE API BASICS</b></p> <p><b>CHAPTER 3: LOADING MODULES 23</b></p> <p>Understanding How Node Loads Modules 24</p> <p>Exporting a Module 24</p> <p>Loading a Module 25</p> <p>Summary 28</p> <p><b>CHAPTER 4: USING BUFFERS TO MANIPULATE, ENCODE, AND DECODE BINARY DATA 29</b></p> <p>Creating a Buffer 30</p> <p>Getting and Setting Bytes in a Buffer 30</p> <p>Slicing a Buffer 32</p> <p>Copying a Buffer 32</p> <p>Decoding a Buffer 32</p> <p>Summary 33</p> <p><b>CHAPTER 5: USING THE EVENT EMITTER PATTERN TO SIMPLIFY EVENT BINDING 35</b></p> <p>Understanding the Standard Callback Pattern 36</p> <p>Understanding the Event Emitter Pattern 36</p> <p>Understanding Event Types 37</p> <p>Using the Event Emitter API 38</p> <p>Binding Callbacks Using .addListener() or .on() 38</p> <p>Binding Multiple Event Listeners 39</p> <p>Removing an Event Listener from an Event Emitter</p> <p>Using .removeListener() 40</p> <p>Getting a Callback Executed at Most Once Using .once() 40</p> <p>Removing All Event Listeners from an Event Emitter Using .removeAllListeners() 41</p> <p>Creating an Event Emitter 41</p> <p>Inheriting from Node Event Emitter 42</p> <p>Emitting Events 42</p> <p>Summary 43</p> <p><b>CHAPTER 6: SCHEDULING THE EXECUTION OF FUNCTIONS USING TIMERS 45</b></p> <p>Using setTimeout to Defer the Execution of a Function 46</p> <p>Using clearTimeout to Cancel the Execution of a Function 46</p> <p>Scheduling and Canceling the Repetitive Execution of a Function 47</p> <p>Using process.nextTick to Defer the Execution of a Function Until the Next Event Loop Iteration 47</p> <p>Blocking the Event Loop 48</p> <p>Escaping the Event Loop 49</p> <p>Using setTimeout Instead of setInterval to Force Serialization 49</p> <p>Summary 50</p> <p><b>PART III: FILES, PROCESSES, STREAMS, AND NETWORKING</b></p> <p><b>CHAPTER 7: QUERYING, READING FROM, AND WRITING TO FILES 53</b></p> <p>Manipulating File Paths 54</p> <p>Normalizing Paths 54</p> <p>Joining Paths 54</p> <p>Resolving Paths 55</p> <p>Finding the Relative Path Between Two Absolute Paths 55</p> <p>Extracting Components of a Path 55</p> <p>Determining the Existence of a Path 56</p> <p>Introducing the fs Module 57</p> <p>Querying File Statistics 57</p> <p>Opening a File 58</p> <p>Reading from a File 59</p> <p>Writing to a File 60</p> <p>Closing a File 60</p> <p>Summary 62</p> <p><b>CHAPTER 8: CREATING AND CONTROLLING EXTERNAL PROCESSES 63</b></p> <p>Executing External Commands 64</p> <p>Spawning Child Processes 69</p> <p>Creating the Child Process 69</p> <p>Listening for Data from the Child Process 69</p> <p>Sending Data to the Child Process 70</p> <p>Receiving Notification When the Child Process Exits 72</p> <p>Signaling and Killing Processes 73</p> <p>Summary 74</p> <p>CHAPTER 9: READING AND WRITING STREAMS OF DATA 75</p> <p>Using a Readable Stream 76</p> <p>Waiting for Data 76</p> <p>Pausing and Resuming a Stream 77</p> <p>Knowing When the Stream Ends 77</p> <p>Using Writable Streams 77</p> <p>Writing Data into a Stream 78</p> <p>Waiting for a Stream to Drain 78</p> <p>Considering Some Stream Examples 78</p> <p>Creating File-System Streams 79</p> <p>Understanding Networking Streams 80</p> <p>Avoiding the Slow Client Problem and Saving Your Server 80</p> <p>Understanding the Slow Client Problem 80</p> <p>Avoiding the Slow Client Problem 81</p> <p>Using stream.pipe() to Prevent the Slow Client Problem and Assembling Readable and Writable Streams Using pipe() 82</p> <p>Summary 82</p> <p><b>CHAPTER 10: BUILDING TCP SERVERS 83</b></p> <p>Creating a TCP Server 83</p> <p>Using the Socket Object 85</p> <p>Understanding Idle Sockets 86</p> <p>Setting Up Keep-Alive 87</p> <p>Using Delay or No Delay 87</p> <p>Listening for Client Connections 88</p> <p>Closing the Server 88</p> <p>Handling Errors 88</p> <p>Building a Simple TCP Chat Server 89</p> <p>Accepting Connections 89</p> <p>Reading Data from a Connection 90</p> <p>Collecting All the Clients 90</p> <p>Broadcasting Data 91</p> <p>Removing Closed Connections 92</p> <p>Using Your TCP Chat Server 93</p> <p>Summary 93</p> <p><b>CHAPTER 11: BUILDING HTTP SERVERS 95</b></p> <p>Understanding the http.ServerRequest Object 97</p> <p>Understanding the http.ServerResponse Object 98</p> <p>Writing a Header 98</p> <p>Changing or Setting a Header 99</p> <p>Removing a Header 99</p> <p>Writing a Piece of the Response Body 99</p> <p>Streaming HTTP Chunked Responses 99</p> <p>Piping a File 100</p> <p>Piping the Output of Another Process 100</p> <p>Shutting Down the Server 101</p> <p>Example 1: Building a Server that Serves Static Files 101</p> <p>Example 2: Making Use of HTTP Chunked Responses and Timers 102</p> <p>Summary 102</p> <p><b>CHAPTER 12: BUILDING A TCP CLIENT 103</b></p> <p>Connecting to a Server 104</p> <p>Sending and Receiving Data 105</p> <p>Ending the Connection 105</p> <p>Handling Errors 106</p> <p>Building an Example Command-Line TCP Client 106</p> <p>Connecting to the Server 107</p> <p>Sending the Command Line to the Server 107</p> <p>Printing Server Messages 107</p> <p>Reconnecting if the Connection Dies 108</p> <p>Closing the Connection 109</p> <p>Putting the Client Together 111</p> <p>Summary 112</p> <p><b>CHAPTER 13: MAKING HTTP REQUESTS 113</b></p> <p>Making GET Requests 113</p> <p>Using Other HTTP Verbs 114</p> <p>Inspecting the Response Object 115</p> <p>Obtaining the Response Body 116</p> <p>Streaming the Response Body 116</p> <p>Pooling Sockets Using http.Agent 116</p> <p>Using a Third-Party Request Module to Simplify HTTP Requests 118</p> <p>Installing and Using Request 118</p> <p>Creating a Testing Server 120</p> <p>Following Redirects 121</p> <p>Setting Some Request Options 123</p> <p>Encoding the Request Body 125</p> <p>Streaming 127</p> <p>Using a Cookie Jar 127</p> <p>Summary 128</p> <p><b>CHAPTER 14: USING DATAGRAMS (UDP) 129</b></p> <p>Understanding UDP 129</p> <p>Understanding the Uses of UDP 130</p> <p>Building a Datagram Server 130</p> <p>Listening for Messages 130</p> <p>Testing the Server 131</p> <p>Inspecting Additional Message Information 132</p> <p>Creating a Simple Datagram Echo Server 132</p> <p>Waiting for Messages 132</p> <p>Sending Messages Back to Senders 132</p> <p>Putting the Echo Server Together 133</p> <p>Building a Datagram Client 134</p> <p>Creating the Client 134</p> <p>Sending Messages 134</p> <p>Closing the Socket 134</p> <p>Creating a Simple Datagram Command-Line Client 135</p> <p>Reading from the Command Line 135</p> <p>Sending Data to the Server 135</p> <p>Receiving Data from the Server 136</p> <p>Putting the Command-Line UDP Client Together 136</p> <p>Understanding and Using Datagram Multicast 136</p> <p>Receiving Multicast Messages 137</p> <p>Sending Multicast Messages 138</p> <p>Understanding Maximum Datagram Size 138</p> <p>Summary 138</p> <p><b>CHAPTER 15: SECURING YOUR TCP SERVER WITH TLS/SSL 139</b></p> <p>Understanding Private and Public Keys 139</p> <p>Generating a Private Key 140</p> <p>Generating a Public Key 140</p> <p>Building a TLS Server 141</p> <p>Initializing the Server 141</p> <p>Listening for Connections 141</p> <p>Reading Data from the Client 142</p> <p>Sending Data to the Client 142</p> <p>Ending the Connection 142</p> <p>Building a TLS Client 143</p> <p>Initializing the Client 143</p> <p>Connecting to the Server 143</p> <p>Verifying the Server Certificate 143</p> <p>Sending Data to the Server 144</p> <p>Reading Data from the Server 144</p> <p>Ending the Connection 144</p> <p>Building Some Examples 145</p> <p>Creating a TLS Chat Server 145</p> <p>Creating a TLS Command-Line Chat Client 146</p> <p>Verifying the Client Certificate 147</p> <p>Summary 148</p> <p><b>CHAPTER 16: SECURING YOUR HTTP SERVER WITH HTTPS 149</b></p> <p>Building a Secure HTTP Server 149</p> <p>Setting Up the Server Options 150</p> <p>Listening for Connections 150</p> <p>Validating the HTTPS Client Certificate 151</p> <p>Creating an HTTPS Client 152</p> <p>Initializing the Client 152</p> <p>Making the Request 152</p> <p>Validating the HTTPS Server Certificate 153</p> <p>Summary 154</p> <p>PART IV: BUILDING AND DEBUGGING MODULES AND APPLICATIONS</p> <p><b>CHAPTER 17: TESTING MODULES AND APPLICATIONS 157</b></p> <p>Using a Test Runner 157</p> <p>Writing Tests 158</p> <p>Running Tests 159</p> <p>Using an Assertion Testing Module 159</p> <p>Using the assert Module 159</p> <p>Using the Built-in Assertion Functions in Node-Tap 161</p> <p>Testing Your Asynchronous Module 163</p> <p>Summary 166</p> <p><b>CHAPTER 18: DEBUGGING MODULES AND APPLICATIONS 167</b></p> <p>Using console.log 167</p> <p>Using Node’s Built-in Debugger 169</p> <p>Using Node Inspector 173</p> <p>Summary 175</p> <p><b>CHAPTER 19: CONTROLLING THE CALLBACK FLOW 177</b></p> <p>Understanding the Boomerang Effect 177</p> <p>Avoiding the Boomerang Effect</p> <p>by Declaring Functions 179</p> <p>Using the async Flow Control Library 183</p> <p>Executing in Series 184</p> <p>Executing in Parallel 185</p> <p>Cascading 186</p> <p>Queuing 187</p> <p>Iterating 189</p> <p>Mapping 190</p> <p>Reducing 191</p> <p>Filtering 192</p> <p>Detecting 193</p> <p>Summary 194</p> <p><b>PART V: BUILDING WEB APPLICATIONS</b></p> <p><b>CHAPTER 20: BUILDING AND USING HTTP MIDDLEWARE 197</b></p> <p>Understanding the Connect HTTP Middleware Framework 198</p> <p>Building Your Own HTTP Middleware 198</p> <p>Creating Asynchronous Middleware 200</p> <p>Registering Callbacks Inside Middleware 201</p> <p>Handling Errors Inside Middleware 203</p> <p>Using the HTTP Middleware Bundled in Connect 206</p> <p>Logging Requests 206</p> <p>Handling Errors 208</p> <p>Serving Static Files 209</p> <p>Parsing the Query String 210</p> <p>Parsing the Request Body 211</p> <p>Parsing Cookies 212</p> <p>Using a Session 213</p> <p>Other Available Middleware 216</p> <p>Summary 216</p> <p><b>CHAPTER 21: MAKING A WEB APPLICATION USING EXPRESS.JS 217</b></p> <p>Initializing Your Express.js Application 218</p> <p>Setting Up Middleware in Your Application 220</p> <p>Routing Requests 222</p> <p>Handling Routes 222</p> <p>Using Sessions 229</p> <p>Using Route Middleware 234</p> <p>Summary 238</p> <p><b>CHAPTER 22: MAKING UNIVERSAL REAL-TIME WEB APPLICATIONS USING SOCKET.IO 241</b></p> <p>Understanding How WebSockets Work 242</p> <p>Using Socket.IO to Build WebSocket Applications 243</p> <p>Installing and Running Socket.IO on the Server 243</p> <p>Building a Real-Time Web Chat with Socket.IO 245</p> <p>Extending the Chat Application 250</p> <p>Detecting Disconnections 254</p> <p>Separating Users into Rooms 255</p> <p>Using Namespaces 259</p> <p>Distributing the Server-Side Application Using Redis 259</p> <p>Summary 263</p> <p><b>PART VI: CONNECTING TO DATABASES</b></p> <p><b>CHAPTER 23: CONNECTING TO MYSQL USING NODE-MYSQL 267</b></p> <p>Using a Library to Connect to and Communicate</p> <p>with a MySQL Database 268</p> <p>Adding Data to the Database with</p> <p>Security Concerns in Mind 270</p> <p>Reading Data Effi ciently 272</p> <p>Summary 276</p> <p><b>CHAPTER 24: CONNECTING TO COUCHDB USING NANO 277</b></p> <p>Installing Nano 278</p> <p>Connecting and Creating a Database 281</p> <p>Storing Documents 285</p> <p>Creating and Using CouchDB Views 286</p> <p>Attaching Files to a CouchDB Document 298</p> <p>Summary 310</p> <p><b>CHAPTER 25: CONNECTING TO MONGODB USING MONGOOSE 311</b></p> <p>Installing Mongoose 313</p> <p>Understanding How Mongoose Uses Models to Encapsulate Database Access 313</p> <p>Connecting to MongoDB 314</p> <p>Defining a Schema 314</p> <p>Defining a Model 315</p> <p>Using Validators 324</p> <p>Using Modifiers 330</p> <p>Using Getters 331</p> <p>Using Virtual Attributes 332</p> <p>Using Default Values 338</p> <p>Defining Indexes 340</p> <p>Referencing Other Documents Using DB Refs 341</p> <p>Defining Instance Methods 347</p> <p>Defining Static Methods 348</p> <p>Summary 349</p> <p>INDEX 351</p>
<p><b>Pedro Teixeira</b> is a prolific open-source programmer and author of many Node.js modules. He is a founding partner of The Node Firm and a senior programmer at Nodejitsu Inc., the leading Node.js platform-as-a-service provider. He is also the author of the popular Node Tuts screencasts.</p> <p><b>Wrox Professional guides</b> are planned and written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.</p>
<p><b>Learn to build scalable software in JavaScript with Node.js</b></p> <p>Node.js is a popular framework that allows you to build fast, highly scalable network programs using JavaScript. However, it comes with a learning curve. This in-depth guide walks you through the installation of the Node.js platform and then focuses on the essentials of creating and loading modules; using buffers to manipulate, encode, and decode binary data; simplifying event binding with the event emitter pattern; and more. Covering everything from scheduling the execution of functions using timers to creating and controlling external processes, this book gets you up and running and building and testing your own modules.</p> <p><i>Professional Node.js:</i></p> <ul> <li>Addresses querying, reading from, and writing to files</li> <li>Explores streams, file systems, networking, and automated unit testing</li> <li>Details how to read and write streams of data</li> <li>Zeroes-in on building TCP and HTTP servers and clients</li> <li>Reviews using datagrams, testing and debugging modules, and controlling the callback flow</li> <li>Shows how to develop real-time web applications using Connect, Express, and Socket.IO</li> <li>Walks you through connecting to MySQL, CouchDB, and MongoDB</li> </ul> <p><b>Programmer Forums</b><br /> Join our Programmer to Programmer forums to ask and answer programming questions about this book, join discussions on the hottest topics in the industry, and connect with fellow programmers from around the world.</p> <p><b>Code Downloads</b><br /> Take advantage of free code samples from this book, as well as code samples from hundreds of other books, all ready to use.</p> <p><b>Read More</b><br /> Find articles, ebooks, sample chapters, and tables of contents for hundreds of books, and more reference resources on programming topics that matter to you.</p>

Diese Produkte könnten Sie auch interessieren:

Domain Architectures
Domain Architectures
von: Daniel J. Duffy
PDF ebook
31,99 €