Histcoroy Pyright for Online Information and Ordering of This and Other Manning Books, Please Visit Topwicws W.Manning.Com
Total Page:16
File Type:pdf, Size:1020Kb
www.allitebooks.com HistCoroy pyright For online information and ordering of this and other Manning books, please visit Topwicws w.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Tutorials Special Sales Department Offers & D e al s Manning Publications Co. 20 Baldwin Road Highligh ts PO Box 761 Shelter Island, NY 11964 Email: [email protected] Settings ©2017 by Manning Publications Co. All rights reserved. Support No part of this publication may be reproduced, stored in a retrieval system, or Sign Out transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acidfree paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. PO Box 761 Shelter Island, NY 11964 www.allitebooks.com Development editor: Cynthia Kane Review editor: Aleksandar Dragosavljević Technical development editor: Stan Bice Project editors: Kevin Sullivan, David Novak Copyeditor: Sharon Wilkey Proofreader: Melody Dolab Technical proofreader: Doug Warren Typesetter and cover design: Marija Tudor ISBN 9781617292576 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17 www.allitebooks.com HistPoray rt 1. Welcome to Node Node is now a mature web development platform. In chapters 1 to 3 , you’ll learn about Topics Node’s main features, including how to use the core modules and npm. You’ll also see Tuthoroiawls Node uses modern JavaScript, and how to build a web application from scratch. After reading these chapters, you’ll have a solid understanding of what Node can do and Offeorfs &h oDwea ltso create your own projects. Highlights Settings Support Sign Out www.allitebooks.com Recommended Chapter 1. Welcome to Node.js Playlists This chapter covers History What is Node.js? Topics Defining Node applications TutorialTs he advantages of using Node Asynchronous and nonblocking I/O Offers & Deals Node.js is an asynchronous, eventdriven JavaScript runtime that offers a powerful but Higcholignhctsise standard library. It’s managed and supported by the Node.js Foundation, an industry consortium with an open governance model. Two actively supported versions Settoinf gNs ode are available: LongTerm Support (LTS) and Current. If you want to learn more about how Node is managed, the official website has plenty of documentation Sup(photrttps://nodejs.org/). SignS Oinutce Node.js appeared in 2009, JavaScript has gone from a barely tolerated browser centric language to one of the most important languages for all kinds of software development. This is partly due to the arrival of ECMAScript 2015, which solved several critical problems in previous versions of the language. Node uses Google’s V8 JavaScript engine that’s based on the sixth edition of the ECMAScript standard, which is sometimes called ES6 and abbreviated as ES2015. It’s also due to innovative technologies such as Node, React, and Electron, which allow JavaScript to be used everywhere: from the server to the browser, and in native mobile applications. Even big companies such as Microsoft are embracing JavaScript, and Microsoft has even contributed to the success of Node. In this chapter, you’ll learn more about Node, its eventdriven nonblocking model, and some of the reasons that JavaScript has become a great generalpurpose programming language. First, let’s look at a typical Node web application. 1.1 . A TYP IC AL NOD E W EB APPLI CATION One of the strengths of Node and JavaScript in general is their singlethreaded www.allitebooks.com pricing options. programming model. Threads are a common source of bugs, and although some recent programming languages, including Go and Rust, have attempted to offer safer concurrency tools, Node retains the model used in the browser. In browserbased code, we write sequences of instructions that execute one at a time; code doesn’t execute in parallel. This doesn’t make sense for user interfaces, however: users don’t want to wait around for slow operations such as network or file access to finish. To get around this, browsers use events: when you click a button, an event fires, and a function runs that has previously been defined but not yet executed. This avoids some of the issues found in threaded programming, including resource deadlocks and race conditions. 1.1.1. Nonblocking I/O What does this mean in the context of serverside programming? The situation is similar: I/O requests such as disk and network access are also comparatively slow, so we don’t want the runtime to block business logic from executing while reading files or sending messages over the network. To solve this, Node uses three techniques: events, asynchronous APIs, and nonblocking I/O. Nonblocking I/O is a lowlevel term from a Node programmer’s perspective. It means your program can make a request for a network resource while doing something else, and then, when the network operation has finished, a callback will run that handles the result. Figure 1.1 shows a typical Node web application that uses the web application library Express to handle the order flow for a shop. Browsers make requests to buy a product, and then the application checks the current stock inventory, creates an account for the user, emails the receipt, and sends back a JSON HTTP response. Concurrently, other things happen as well: an email receipt is sent, and a database is updated with the user’s details and order. The code itself is straightforward, imperative JavaScript, but the runtime behaves concurrently because it uses nonblocking I/O. Figure 1.1. Asynchronous and nonblocking components in a Node application In figure 1.1 the database is accessed over the network. In Node, that network access is nonblocking, because Node uses a library called libuv (http://libuv.org/) to provide access to the operating system’s nonblocking network calls. This is implemented differently in Linux, macOS, and Windows, but all you have to worry about is your friendly Java Script database library. While you’re writing code such as db.insert(query, err => {}), Node is doing highly optimized, nonblocking networking underneath. Disk access is similar, but intriguingly not the same. When the email receipt is generated and the email template is read from the disk, libuv uses a thread pool to provide the illusion that a nonblocking call is being used. Managing a thread pool is no fun at all, but writing email.send('template.ejs', (err, html) => {}) is definitely much easier to understand. The real benefit to using asynchronous APIs with nonblocking I/O is that Node can do other things while these comparatively slow processes happen. Even though you have only a singlethreaded, singleprocess Node web app running, it can handle more than one connection from potentially thousands of website visitors at any one time. To understand this, you need to look at the event loop. 1.1.2. The event loop Now let’s zoom into a specific aspect of figure 1.1: responding to browser requests. In this application, Node’s builtin HTTP server library, which is a core module called http.Server, handles the request by using a combination of streams, events, and Node’s HTTP request parser, which is native code. This triggers a callback in your application to run, which has been added using the Express (https://expressjs.com/) web application library. The callback that runs causes a database query to run, and eventually the application responds with JSON using HTTP. This whole process uses a minimum of three nonblocking network calls: one for the request, one for the database, and another for the response. How does Node schedule all these nonblocking network operations? The answer is the event loop. Figure 1.2 shows how the event loop is used for these three network operations. Figure 1.2. The event loop The event loop runs one way (it’s a firstin, firstout queue) and goes through several phases. Figure 1.2 shows a simplified set of the important phases that run on each iteration of the loop. First, the timers execute, which are the timers scheduled with the JavaScript functions setTimeout and setInterval. Next, I/O callbacks run, so if any I/O has returned from one of the nonblocking network calls, this is where your callback is triggered. The poll phase is where new I/O events are retrieved, and then callbacks scheduled with setImmediate run at the end. This is a special case because it allows you to schedule a callback to run immediately after the current I/O callbacks already in the queue. This might sound abstract at this stage, but what you should take away is the idea that although Node is singlethreaded, it does give you tools to write efficient and scalable code. Over the last few pages, you might have noticed that the examples have been written using ES2015 arrow functions. Node supports many new JavaScript features, so before moving on, let’s look at what new language features you can use to write better code.