Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Node.js Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3. Modules and npm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4. Node’s Programming Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5. Core Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 6. Building the Node Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 7. MongoDB Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 8. Interacting with MongoDB Using Mongoose . . . . . . . . . . . . . . . . . . . . . . . . . 99 9. Using MongoDB and Mongoose in Our Sample App . . . . . . . . . . . . . . . . . 115 10. Alternatives to Mongo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 11. Introduction to Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 12. Architecture of an Express Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 13. Using Express in Our App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 14. Alternative Server Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 15. AngularJS Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 16. Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 17. Angular Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 18. Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 19. Client-side Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 20. Angular in Our App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 21. Task Runners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 22. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 23. Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 FULL STACK JAVASCRIPT DEVELOPMENT WITH MEAN BY ADAM BRETZ & COLIN J. IHRIG iv Full Stack JavaScript Development with MEAN byAdamBretzandColinJ.Ihrig Copyright©2014SitePointPty.Ltd. Product Manager:SimonMackie English Editor:KellySteele Technical Editor:DonNguyen Cover Designer:AlexWalker Notice of Rights Allrightsreserved.Nopartofthisbookmaybereproduced,storedinaretrievalsystemortransmitted inanyformorbyanymeans,withoutthepriorwrittenpermissionofthepublisher,exceptinthecase ofbriefquotationsembodiedincriticalarticlesorreviews. Notice of Liability Theauthorandpublisherhavemadeeveryefforttoensuretheaccuracyoftheinformationherein. However,theinformationcontainedinthisbookissoldwithoutwarranty,eitherexpressorimplied. NeithertheauthorsandSitePointPty.Ltd.,noritsdealersordistributorswillbeheldliableforany damagestobecausedeitherdirectlyorindirectlybytheinstructionscontainedinthisbook,orbythe softwareorhardwareproductsdescribedherein. Trademark Notice Ratherthanindicatingeveryoccurrenceofatrademarkednameassuch,thisbookusesthenamesonly inaneditorialfashionandtothebenefitofthetrademarkownerwithnointentionofinfringementof thetrademark. PublishedbySitePointPty.Ltd. 48CambridgeStreetCollingwood VICAustralia3066 Web:www.sitepoint.com Email:[email protected] ISBN978-0-9924612-5-6(print) ISBN978-0-9924612-4-9(ebook) PrintedandboundintheUnitedStatesofAmerica v About Adam Bretz AdamBretzisasoftwareengineerfocusingonclientandserversideJavaScript.Adamearned hisBachelorofScienceinComputerSciencein2007fromMillersvilleUniversityof Pennsylvania.Atapreviousjob,Adamwaspartoftheteamofengineersthathelpedmigrate thecompanyfromPHPtoapureJavaScriptsolution.AdamcurrentlyresidesinthePittsburgh areawithhiswife,Jenna. About Colin J. Ihrig ColinJ.Ihrigisasoftwareengineer,workingprimarilywithNode.js.Colinistheauthorof ProNode.jsforDevelopers,andiscurrentlythemanagingeditorofSitePoint'sJavaScript channel.ColinreceivedhisBachelorofScienceinEngineering,andMasterofSciencein ComputerEngineeringfromtheUniversityofPittsburghin2005and2008,respectively. About SitePoint SitePointspecializesinpublishingfun,practical,andeasy-to-understandcontentforweb professionals.Visithttp://www.sitepoint.com/toaccessourblogs,books,newsletters,articles, andcommunityforums.You’llfindastackofinformationonJavaScript,PHP,Ruby,mobile development,design,andmore. To Mom and Dad ― thanks for getting me a Nintendo when I was seven and a computer when I was ten. ― Adam This book is dedicated to my wife, Alaina, my sons, CJ and Carter, and my mom. I love you all so much! ― Colin Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Conventions Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Supplementary Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Want to Take Your Learning Further? . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Chapter 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Rise of Full-stack JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Node.js Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 MongoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Chapter 2 Node.js Introduction . . . . . . . . . . . . . . . . . . . . 11 Familiarity with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Problem with I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 An Example Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Stepping Around the I/O Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Real World Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Your First Node.js Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Writing the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Description: