Node.js in Action
Mike Cantelon, Marc Harter, T.J. Holowaychuk and Nathan Rajlich
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or 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 Mannings policy to have the books we publish printed on acid-free 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.20 Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editor: Renae GregoireCopyeditor: Andy CarrollProofreader: Katie TennantTypesetter: Dottie MarsicoCover designer: Marija Tudor |
ISBN 9781617290572
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 18 17 16 15 14 13
Brief Table of Contents
Table of Contents
Foreword
Writing a book about Node.js is a challenging endeavor. Its a relatively new platform, just now attaining stability. The Node.js core continues to evolve, and the community of user-created modules is exploding at a pace that no one can hope to keep track of. The community is still finding its voice. The only way to catch such a moving target in print is to get at the essence of what Node is, and why it has been succeeding as it has. These Node.js veterans have done just that. Mike Cantelon is extremely active in the Node community, experimenting with and speaking about Node. He has an excellent grasp on what Node is good for, and perhaps more importantly, what it isnt good for. T.J. Holowaychuk is one of the most prolific authors of Node.js modules, including the massively popular Express web framework. Nathan Rajlich, better known to many as TooTallNate, has been a Node.js core committer for some time now and is an active part of the development of the platform as it has matured into its current state.
This book draws on their considerable experience, taking you from the very first steps of installing Node.js on your computer all the way to creating, debugging, and deploying production applications. Youll learn what makes Node interesting and get a glimpse into the authors combined understanding, so that the future directions the Node project takes will make sense. Most importantly, the content ramps up nicely from basic to advanced, building on prior learning at each stage.
Node is an ascending rocket, and the authors have done a great job of bringing you along for the ride. Think of Node.js in Action as the launching pad from which to explore your own horizons.
I SAAC Z. S CHLUETER
NPM AUTHOR
N ODE.JS P ROJECT L EAD
Preface
In early 2011, when Manning approached us with the idea of writing a book on Node.js, the state of the Node community was much different than it is now. The community was small and, despite the fact that Node was starting to attract a great deal of interest, Node was still considered a bleeding-edge technology by the mainstream development community. No books had yet been written about it, and although the idea of writing a book was daunting, we decided to go for it.
Given our respective development inclinations, we wanted to create a book that not only focused on the use of Node for web application development, but also explored other interesting potential uses. We wanted to provide a way for web application developers using conventional technologies to harness Nodes vision of bringing asynchronous development to the server.
Weve worked for more than two years on the book, and during its writing the technology has evolved, so weve updated the book accordingly. In addition to the technology changing, the community has also evolved. It is now much larger, and many established companies have embraced Node.
For web application developers looking to try something different, this is a great time to learn Node, and we hope our book helps you learn the technology quickly and have fun doing so.
Acknowledgments
Thanks are due to the great people at Manning for their role in the creation of this book. Renae Gregoire played a major role, pushing us toward eloquence, clarity, and quality. Bert Bates helped define the books visual feel, working with us to design graphics expressing various concepts presented in the book. Marjan Bace and Michael Stephens believed in us enough to entrust us with the creation of the book and helped keep the project moving. And Mannings editorial, production, and technical staff were amazing to work with.
Many people reviewed the manuscript in various stages of its development, and we would like to thank them for their feedback. This includes our MEAP readers who posted comments and corrections in the books online forum, and the following reviewers who read the manuscript multiple times and whose insights and comments helped make this a much better book: lex Madurell, Bert Thomas, Bradley Meck, Braj Panda, Brian L. Cooley, Brian Del Vecchio, Brian Dillard, Brian Ehmann, Brian Falk, Daniel Bretoi, Gary Ewan Park, Jeremy Martin, Jeroen Nouws, Jeroen Trappers, Kassandra Perch, Kevin Baister, Michael Piscatello, Patrick Steger, Paul Stack, and Scott Banachowski.
Thanks also to Valentin Crettaz and Michael Levin for their careful technical proofread of the final manuscript, shortly before it went into production. Last but not least, wed like to thank Isaac Schlueter, Node Project Lead, for contributing the foreword to our book.
M IKE C ANTELON
Id like to thank my friend Joshua Paul for giving me my first break in the tech industry, introducing me to the world of open source, and encouraging me to write a book. Id also like to thank my partner Malcolm for encouraging me during the books creation and for her patience during the times when writing kept me constantly cooped up at home. A big thanks, as well, to my parents for bringing me up with a passion for creativity and exploration, and for putting up with my less-than-balanced childhood obsession with 8-bit machines. Id also like to thank my grandparents for gifting me with the machine that got me hooked, for life, on programming: the Commodore 64.
During the process of writing the book, T.J. and Nathans expertise was invaluable and their good humor much appreciated. I thank them for taking a leap of faith and agreeing to collaborate. Marc Harter was also a huge help, chipping in on the Herculean task of editing, proofing, and writing content that tied everything together.
M ARC H ARTER
Thanks to Ryan Dahl, who inspired me to take a serious look at server-side JavaScript programming nearly four years ago. Thanks to Ben Noordhuis, an invaluable resource on the inner workings of Node. Thanks to Bert Bates, who believed in me, challenged me, and was always willing to help during the writing process. Thanks to Mike, Nate, and T.J. for welcoming me in at the 11th hour. It was an honor working with them. Thanks especially to my wife and friend Hannah, whose courage and kindness carried me into and through this new venture.