The-eye.eu

Transcription

Summary of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12. Node.js Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113. Modules and npm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254. Node’s Programming Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415. Core Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596. Building the Node Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737. MongoDB Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878. Interacting with MongoDB Using Mongoose . . . . . . . . . . . . . . . . . . . . . . . . . 999. Using MongoDB and Mongoose in Our Sample App . . . . . . . . . . . . . . . . . 11510. Alternatives to Mongo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12911. Introduction to Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14112. Architecture of an Express Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 15313. Using Express in Our App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16714. Alternative Server Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17515. AngularJS Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18716. Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19717. Angular Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20918. Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21719. Client-side Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22920. Angular in Our App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23921. Task Runners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25722. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26923. Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

FULL STACKJAVASCRIPTDEVELOPMENTWITH MEANBY ADAM BRETZ& COLIN J. IHRIG

ivFull Stack JavaScript Development with MEANby Adam Bretz and Colin J. IhrigCopyright 2014 SitePoint Pty. Ltd.Product Manager: Simon MackieEnglish Editor: Kelly SteeleTechnical Editor: Don NguyenCover Designer: Alex WalkerNotice of RightsAll rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmittedin any form or by any means, without the prior written permission of the publisher, except in the caseof brief quotations embodied in critical articles or reviews.Notice of LiabilityThe author and publisher have made every effort to ensure the accuracy of the information herein.However, the information contained in this book is sold without warranty, either express or implied.Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for anydamages to be caused either directly or indirectly by the instructions contained in this book, or by thesoftware or hardware products described herein.Trademark NoticeRather than indicating every occurrence of a trademarked name as such, this book uses the names onlyin an editorial fashion and to the benefit of the trademark owner with no intention of infringement ofthe trademark.Published by SitePoint Pty. Ltd.48 Cambridge Street CollingwoodVIC Australia 3066Web: www.sitepoint.comEmail: business@sitepoint.comISBN 978-0-9924612-5-6 (print)ISBN 978-0-9924612-4-9 (ebook)Printed and bound in the United States of America

vAbout Adam BretzAdam Bretz is a software engineer focusing on client and server side JavaScript. Adam earnedhis Bachelor of Science in Computer Science in 2007 from Millersville University ofPennsylvania. At a previous job, Adam was part of the team of engineers that helped migratethe company from PHP to a pure JavaScript solution. Adam currently resides in the Pittsburgharea with his wife, Jenna.About Colin J. IhrigColin J. Ihrig is a software engineer, working primarily with Node.js. Colin is the author ofPro Node.js for Developers, and is currently the managing editor of SitePoint's JavaScriptchannel. Colin received his Bachelor of Science in Engineering, and Master of Science inComputer Engineering from the University of Pittsburgh in 2005 and 2008, respectively.About SitePointSitePoint specializes in publishing fun, practical, and easy-to-understand content for webprofessionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles,and community forums. You’ll find a stack of information on JavaScript, PHP, Ruby, mobiledevelopment, design, and more.

To Mom and Dad ― thanks forgetting me a Nintendo when I wasseven and a computer when I wasten. ― AdamThis book is dedicated to my wife,Alaina, my sons, CJ and Carter,and my mom. I love you all somuch! ― Colin

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixWho Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixConventions Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxCode Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxTips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiSupplementary Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiWant to Take Your Learning Further? . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiChapter 1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1The Rise of Full-stack JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Node.js Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4MongoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5AngularJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Chapter 2Node.js Introduction . . . . . . . . . . . . . . . . . . . . 11Familiarity with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Problem with I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .An Example Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Stepping Around the I/O Problem . . . . . . . . . . . . . . . . . . . . . . . . . . .Real World Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Your First Node.js Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Writing the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111314161719191920

xOur Server in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Chapter 3Modules and npm . . . . . . . . . . . . . . . . . . . . . . . 25npm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .npm install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .npm search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .package.json . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26262829The node modules Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Module Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31require() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Other Uses for require . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Writing a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Module Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38npm link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Chapter 4Node’s Programming Model . . . . . . . . . . 41The Event Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41The Illusion of Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Asynchronous Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Calling Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Callback Hell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Event Emitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Extending EventEmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Listening for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

xiThe uncaughtException Event . . . . . . . . . . . . . . . . . . . . . . . . . . .Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Promise Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53545657Core Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59Chapter 5Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Working with the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60filename and dirname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60The Current Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Readable Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Writable Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Web Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Creating a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Accessing Request Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Chapter 6Building the Node Server . . . . . . . . . . . . . 73Server Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Structuring the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Database Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Querying the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Response Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

xiiPutting It Back Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Chapter 7MongoDB Introductio

Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1. Introduction