Learning Node 1st Edition Shelley Powers - Instantly access the complete ebook with just one click
Learning Node 1st Edition Shelley Powers - Instantly access the complete ebook with just one click
com
https://ebookname.com/product/learning-node-1st-edition-
shelley-powers/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/learning-javascript-2nd-edition-
shelley-powers/
https://ebookname.com/product/node-js-blueprints-tsonev/
https://ebookname.com/product/node-js-by-example-1st-edition-
tsonev/
https://ebookname.com/product/alternative-digital-
photography-1st-edition-john-g-blair/
SAS R 9 1 3 Language Reference Concepts Second Edition
2 Volume Set Inc Sas Institute
https://ebookname.com/product/sas-r-9-1-3-language-reference-
concepts-second-edition-2-volume-set-inc-sas-institute/
https://ebookname.com/product/political-parties-and-terrorist-
groups-2nd-ed-extremism-and-democracy-2nd-edition-leonar-
weinberg/
https://ebookname.com/product/postcolonial-literature-1st-
edition-dave-gunning/
https://ebookname.com/product/inspiration-1st-edition-david-r-
law/
https://ebookname.com/product/plate-tectonics-steve-tomecek/
A Very Social Time Crafting Community in Antebellum New
England Karen V. Hansen
https://ebookname.com/product/a-very-social-time-crafting-
community-in-antebellum-new-england-karen-v-hansen/
Learning Node
Shelley Powers
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or [email protected].
Editor: Simon St. Laurent Indexer: Aaron Hazelton, BIM Publishing Services
Production Editor: Rachel Steely Cover Designer: Karen Montgomery
Copyeditor: Rachel Monaghan Interior Designer: David Futato
Proofreader: Kiel Van Horn Illustrators: Robert Romano and Rebecca Demarest
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Node, the image of a hamster rat, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-32307-3
[LSI]
1345837731
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
iii
Servers, Streams, and Sockets 41
TCP Sockets and Servers 42
HTTP 44
UDP/Datagram Socket 46
Streams, Pipes, and Readline 48
Child Processes 50
child_process.spawn 50
child_process.exec and child_process.execFile 52
child_process.fork 53
Running a Child Process Application in Windows 53
Domain Resolution and URL Processing 54
The Utilities Module and Object Inheritance 56
Events and EventEmitter 59
iv | Table of Contents
7. The Express Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Express: Up and Running 128
The app.js File in More Detail 129
Error Handling 132
A Closer Look at the Express/Connect Partnership 133
Routing 134
Routing Path 136
Routing and HTTP Verbs 139
Cue the MVC 145
Testing the Express Application with cURL 150
Table of Contents | v
Refactoring the Widget Factory 222
Adding the MongoDB Backend 223
vi | Table of Contents
Acceptance Testing 301
Selenium Testing with Soda 301
Emulating a Browser with Tobi and Zombie 305
Performance Testing: Benchmarks and Load Tests 306
Benchmark Testing with ApacheBench 307
Load Testing with Nodeload 311
Refreshing Code with Nodemon 313
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Why Node?
If you explore the source code for Node, you’ll find the source code for Google’s V8,
the JavaScript (technically, ECMAScript) engine that’s also at the core of Google’s
Chrome browser. One advantage to Node.js, then, is that you can develop Node ap-
plications for just one implementation of JavaScript—not half a dozen different brows-
ers and browser versions.
Node is designed to be used for applications that are heavy on input/output (I/O), but
light on computation. More importantly, it provides this functionality directly out of
the box. You don’t have to worry about the application blocking any further processing
ix
while waiting for a file to finish loading or a database to finish updating, because most
of the functionality is asynchronous I/O by default. And you don’t have to worry about
working with threads, because Node is implemented on a single thread.
Most importantly, Node is written in a language that many traditional web developers
are familiar with: JavaScript. You may be learning how to use new technologies, such
as working with WebSockets or developing to a framework like Express, but at least
you won’t have to learn a new language along with the concepts. This language famil-
iarity makes it a lot easier to just focus on the new material.
If you’re not sure you’re familiar enough with JavaScript, you might
want to check out my introductory text on JavaScript, Learning Java-
Script, Second Edition (O’Reilly).
x | Preface
How to Best Use This Book
You don’t have to read this book’s chapters in order, but there are paths through the
book that are dependent on what you’re after and how much experience you have with
Node.
If you’ve never worked with Node, then you’re going to want to start with Chapter 1
and read through at least Chapter 5. These chapters cover getting both Node and the
package manager (npm) installed, how to use them, creating your first applications,
and utilizing modules. Chapter 5 also covers some of the style issues associated with
Node, including how to deal with Node’s unique approach to asynchronous develop-
ment.
If you have had some exposure to Node, have worked with both the built-in Node
modules and a few external ones, and have also used REPL (read-eval-print loop—the
interactive console), you could comfortably skip Chapter 1–Chapter 4, but I still
recommend starting no later than Chapter 5.
I incorporate the use of the Express framework, which also utilizes the Connect mid-
dleware, throughout the book. If you’ve not worked with Express, you’re going to want
to go through Chapter 6–Chapter 8, which cover the concepts of routing, proxies, web
servers, and middleware, and introduce Express. In particular, if you’re curious about
using Express in a Model-View-Controller (MVC) framework, definitely read Chap-
ter 7 and Chapter 8.
After these foundation chapters, you can skip around a bit. For instance, if you’re
primarily working with key/value pairs, you’ll want to read the Redis discussion in
Chapter 9; if you’re interested in document-centric data, check out Chapter 10, which
introduces how to use MongoDB with Node. Of course, if you’re going to work only
with a relational database, you can go directly to Chapter 11 and skip the Redis and
MongoDB chapters, though do check them out sometime—they might provide a new
viewpoint to working with data.
After those three data chapters, we get into specialized application use. Chapter 12
focuses purely on graphics and media access, including how to provide media for the
new HTML5 video element, as well as working with PDF documents and Canvas.
Chapter 13 covers the very popular Sockets.io module, especially for working with the
new web socket functionality.
After the split into two different specialized uses of Node in Chapter 12 and Chap-
ter 13, we come back together again at the end of the book. After you’ve had some time
to work with the examples in the other chapters, you’re going to want to spend some
in Chapter 14, learning in-depth practices for Node debugging and testing.
Chapter 15 is probably one of the tougher chapters, and also one of the more important.
It covers issues of security and authority. I don’t recommend that it be one of the first
Preface | xi
chapters you read, but it is essential that you spend time in this chapter before you roll
a Node application out for general use.
Chapter 16 is the final chapter, and you can safely leave it for last, regardless of your
interest and experience. It focuses on how to prepare your application for production
use, including how to deploy your Node application not only on your own system, but
also in one of the cloud servers that are popping up to host Node applications. I’ll also
cover how to deploy a Node application to your server, including how to ensure it plays
well with another web server such as Apache, and how to ensure your application
survives a crash and restarts when the system is rebooted.
Node is heavily connected with the Git source control technique, and most (if not all)
Node modules are hosted on GitHub. The Appendix provides a Git/GitHub survival
guide for those who haven’t worked with either.
I mentioned earlier that you don’t have to follow the chapters in order, but I recommend
that you do. Many of the chapters work off effort in previous chapters, and you may
miss out on important points if you skip around. In addition, though there are numer-
ous standalone examples all throughout the book, I do use one relatively simple Express
application called Widget Factory that begins life in Chapter 7 and is touched on, here
and there, in most of the rest of the chapters. I believe you’ll have a better time with
the book if you start at the beginning and then lightly skim the sections that you know,
rather than skip a chapter altogether.
As the king says in Alice in Wonderland, “Begin at the beginning and go on till you come
to the end: then stop.”
The Technology
The examples in this book were created in various releases of Node 0.6.x. Most were
tested in a Linux environment, but should work, as is, in any Node environment.
Node 0.8.x released just as this book went to production. The examples in the chapters
do work with Node 0.8.x for the most part; I have indicated the instances where you’ll
need to make a code change to ensure that the application works with the newest Node
release.
The Examples
You can find the examples as a compressed file at the O’Reilly web page for this book
(http://oreil.ly/Learning_node). Once you’ve downloaded and uncompressed it, and
you have Node installed, you can install all the dependency libraries for the examples
by changing to the examples directory and typing:
npm install -d
I’ll cover more on using the Node package manager (npm) in Chapter 4.
xii | Preface
Conventions Used in This Book
The following typographical conventions are used in this book:
Plain text
Indicates menu titles, menu options, menu buttons, and keyboard accelerators
(such as Alt and Ctrl).
Italic
Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames,
directories, and Unix utilities.
Constant width
Indicates commands, options, switches, variables, attributes, keys, functions,
types, classes, namespaces, methods, modules, properties, parameters, values, ob-
jects, events, event handlers, XML tags, HTML tags, macros, the contents of files,
or the output from commands.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values.
Preface | xiii
Other documents randomly have
different content
HARRODS, Ltd.
HARRODS SADDLE SOAP SADDLE SOAP
HARROD’S LIMITED Harrods’ Saddle Soap.
SADDLE SOAP In bars.
Harrods’ Saddle Soap. Per bar ... ... ... 0/6
Per tin ... 0/6 Per doz. bars ... 5/6
Per doz. ... 5/6
BRILLIANT
Metallic Powder
HARRODS Ltd. LONDON
GREEN REVIVER
Brilliant Metallic Powder. HARDING’S MOROCCO REVIVER
For cleaning brass, copper, etc. HARDING & Cº
Per box ... 0/6 Per doz. ... 5/6 Morocco Leather
Per lb. tin 1/0 Per doz. ... 10/6 Reviver (Harding’s).
In various colours.
Per bottle ... 1/0
AXLE OIL HARRODS. LIMITED
HARRODS, LIMITED VETERINARY
Brompton Road. London s.w. HOOF DRESSING
Harrods’ Axle Oil. BROMPTON ROAD. LONDON
Per pint ... 0/9 Harrods’ Veterinary
,, quart ... 1/4 Hoof Dressing.
,, ½ gallon ... 2/0 Per pint tin ... 1/6
,, gallon ... 3/6 ,, quart tin ... 2/9
,, ½ gallon tin ... 5/0
,, gallon tin ... 9/0
Jet=Black HARNESS OIL
Harrods’ Black Oil.
Per pint ... 0/10½
,, quart ... 1/6
THE Nortthcote Preparation
,, ½ gallon ... 2/9
Scarlet Coats
,, gallon ... 4/6 T.C. MANN
Mann’s Scarlet Coat Preparation.
Per bottle ... 2/6
MANN’S MANN’S
Nortthcote Breeches and Glove Paste THE NORTTHCOTE BREECHES POLISH
27 16 8 95/0 33 17 9½ 122/6
The 33 in. has 2 end handles.
No. S A 1504. Shallow Imperial or best butt leather
Steamer Trunk, turn-over edge, extra large solid leather-
capped corners, steel banded and hand-sewn, fitted with tray,
strong lever lock and 2 keys, extra stout luggage straps all
round, leather ledges, etc.
Length. Width. Depth. Price. Length. Width. Depth. Price.
30 in. 21 in. 12½ in. 126/0 36 in. 23 in. 12½ in. 145/0
33 22 12½ 135/0
No. S A 1505. Shallow Imperial or Cabin Trunk, as above,
No. 2 quality.
Length. Width. Depth. Price. Length. Width. Depth. Price.
27 in. 20 in. 12½ in. 79/0 33 in. 22 in. 12½ in. 95/0
30 21 12½ 86/0 36 23 12½ 104/0
HARRODS Ltd.
No. S A 1506. The Imperial Trunk, made of best butt leather,
turn-over edge, extra large solid leather-capped corners, steel
banded, and hand-sewn throughout, fitted with tray, strong
lever lock and 2 keys extra stout luggage straps all round.
Length. Width. Depth. Price. Length. Width. Depth. Price.
30 in. 19 in. 16 in. 130/0 36 in. 20½ in. 18 in. 152/6
33 19½ 17 139/6
No. S A 1507. The Imperial Trunk, as above, No. 2 quality.
Length. Width. Depth. Price. Length. Width. Depth. Price.
30 in. 19 in. 16 in. 86/0 36 in. 20½ in. 18 in. 104/0
33 19½ 17 95/0
No. S A 1508. P. & O. Cabin Trunk, made of extra stout
portmanteau hide welted edge, 4 bottom capped corners, lever
lock, 2 keys, straps all round, with tray.
36 in. long and 21 in. wide × 14 in. deep, 97/6
HARRODS Ltd.
No. S A 1532. LADIES’ HAT BOX.
Superior quality, covered extra strong Green or
Brown Canvas, large Leather Corners, Lever Lock
and two end clips.
Size. Price.
24 in. Long × 20 in. Wide × 20 in. Deep, 60/0
Ditto as Illustration, with Long Strap all round
and End Handles.
Size. Price.
26 in. Long × 22 in. Wide × 22 in. Deep, 70/0
28 in. Long × 24 in. Wide × 24 in. Deep, 77/6
HARRODS Ltd.
No. S A 1541. LADIES’ HAT BOX, Patent Compressed
Cane, fitted with 6 Patent movable Gauze Wire Fittings.
Size 20 in. × 18 in. × 18 in., 50/9
,, 22 in. × 18 in. × 18 in., 53/6
,, 22 in. × 20 in. × 20 in., 58/9
,, 24 in. × 22 in. × 22 in., 70/6
,, 26 in. × 24 in. × 24 in., 81/6
HARRODS Ltd.
No. S A 1533. LADIES’ HAT BOX.
Made in Green Willesden, Brown or Black
Canvas, stiffened on “Flaxite” fibre, fitted with
Patent Removable Wire Fittings to affix Hats or
Bonnets.
Size. Price.
20 in. Long × 16 in. Wide × 16 in. Deep
to take 6 Hats. 27/6
22 in. Long × 18 in. Wide × 18 in. Deep 34/6
24 in. Long × 20 in. Wide × 20 in. Deep 42/0
26 in. Long × 22 in. Wide × 22 in. Deep 47/6
No. S A 1534. As above, but stronger make, and
large Leather Corners.
Size. Price.
24 in. Long × 20 in. Wide × 20 in. Deep 47/6
26 in. Long × 22 in. Wide × 22 in. Deep 55/0
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com