About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Learning Node.js
A Hands-On Guide to Building Web Applications in JavaScript
Marc Wandschneider
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Cape Town Sydney Tokyo Singapore Mexico City
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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2013936406
Copyright 2013 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-91057-8
ISBN-10: 0-321-91057-5
Text printed in the United States on recycled paper at RR Donnelley & Sons, Crawfordsville, Indiana.
First printing: June 2013
Editor-in-Chief
Mark Taub
Acquisitions Editor
Mark Taub
Marketing Manager
Stephane Nakib
Managing Editor
Kristy Hart
Senior Project Editor
Lori Lyons
Copy Editor
Chuck Hutchinson
Indexer
Tim Wright
Proofreader
Anne Goebel
Technical Reviewer
Bill Glover
Editorial Assistant
Olivia Basegio
Cover Designer
Chuti Prasertsith
Compositor
Nonie Ratcliff
Manufacturing Buyer
Dan Uhrig
Much love to Tina, for simply being there.
Acknowledgments
Id like to thank all the Marks at PHPTR (its a common name, it seems) who have helped me make this book and other projects a reality. The copy editors have been brilliant and helpful.
A huge debt of gratitude is due to Bill Glover and Idriss Juhoor for their excellent technical and style reviews.
And finally, much love to Tina, for simply being there.
About the Author
Marc Wandschneider co-founded Adylitica, a leading specialist in massively scalable web and mobile app design and development. He travels the globe, consulting as a lead manager for software projects and teams. A graduate of McGill Universitys School of Computer Science, he spent five years at Microsoft, developing and managing developers on the Visual Basic, Visual J++, and .NET Windows Forms teams. As Contract Software Developer/Architect at SourceLabs, he built the SWiK open source Wiki platform. He authored PHP and MySQL LiveLessons and Core Web Application Development with PHP and MySQL.
Introduction
Welcome to Learning Node.js. Node.js is an exciting new platform for writing network and web applications that has created a lot of buzz over the past couple of years and rapidly gathered a sizeable following in the developer community. In this book, I teach you more about it, why it is special, and get you up and writing Node.js programs in short order. Youll soon find that people are rather flexible with the name of Node.js and will refer to it frequently as just Node or even node. I certainly do a lot of that in this book as well.
Why Node.js?
Node.js has arisen for a couple of primary reasons, which I explain next.
The Web
In the past, writing web applications was a pretty standard process. You have one or more servers on your machine that listens on a port (for example, 80 for HTTP), and when a request is received, it forks a new process or a thread to begin processing and responding to the query. This work frequently involves communicating with external services, such as a database, memory cache, external computing server, or even just the file system. When all this work is finally finished, the thread or process is returned to the pool of available servers, and more requests can be handled.
It is a reasonably linear process, easy to understand, and straightforward to code. There are, however, a couple of disadvantages that continue to plague the model:
Each of these threads or processes carries some overhead with it. On some machines, PHP + Apache can take up as much as 1015MB per process. Even in environments where a large server runs constantly and forks threads to process the requests, each of these carries some overhead to create a new stack and execution environment, and you frequently run into the limits of the servers available memory.
In most common usage scenarios where a web server communicates with a database, caching server, external server, or file system, it spends most of its time sitting around doing nothing and waits for these services to finish and return their responses. While it is sitting there doing nothing, this thread is effectively blocked from doing anything else. The resources it consumes and the process or thread in which it runs are entirely frozen waiting for those responses to come back.