Pro-Express.Js.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
dan www.allitebooks.com For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.allitebooks.com Contents at a Glance About the Author ................................................................................................................ xv About the Technical Reviewers ........................................................................................ xvii Foreword ........................................................................................................................... xix Acknowledgments ............................................................................................................. xxi Introduction ..................................................................................................................... xxiii ■ Part I: Getting Started ................................................................................... 1 ■ Chapter 1: Starting with Express.js ...................................................................................3 ■ Chapter 2: Hello World Example ......................................................................................15 ■ Part II: Deep API Reference ........................................................................ 31 ■ Chapter 3: Configuration, Settings, and Environments ....................................................33 ■ Chapter 4: Working with Middleware ..............................................................................51 ■ Chapter 5: Template Engines and Consolidate.js .............................................................75 ■ Chapter 6: Parameters and Routing ................................................................................87 ■ Chapter 7: Express.js Request Object ............................................................................103 ■ Chapter 8: Express.js Response Object .........................................................................119 ■ Chapter 9: Error Handling and Running an App .............................................................141 iii www.allitebooks.com ■ CONTENTS AT A GLANCE ■ Part III: Solving Common and Abstract Problems .................................... 153 ■ Chapter 10: Abstraction.................................................................................................155 ■ Chapter 11: Database, Keys and Stream Tips ................................................................161 ■ Chapter 12: Redis and Authentication Patterns ............................................................171 ■ Chapter 13: Multithreading with Clusters .....................................................................177 ■ Chapter 14: Applying Stylus, Less, and Sass .................................................................181 ■ Chapter 15: Security Tips ..............................................................................................185 ■ Chapter 16: Socket.IO and Express.js ............................................................................193 ■ Chapter 17: Domain and Express.js ...............................................................................199 ■ Chapter 18: Sails.js, DerbyJS, LoopBack, and Other Frameworks ................................205 ■ Part IV: Tutorials and Examples ................................................................ 215 ■ Chapter 19: Instagram Gallery ......................................................................................217 ■ Chapter 20: Todo App ....................................................................................................223 ■ Chapter 21: REST API.....................................................................................................249 ■ Chapter 22: HackHall .....................................................................................................263 ■ Appendix A: Related Reading and Resources ................................................................317 ■ Appendix B: Migrating Express.js 3.x to 4.x: Middleware, Route, and Other Changes ...............................................................................................................321 ■ Appendix C: Express.js 4 Cheat Sheet ...........................................................................329 ■ Appendix D: ExpressWorks ............................................................................................335 Index .................................................................................................................................339 iv www.allitebooks.com Introduction If you are considering whether or not to buy this book, this Introduction will help you to make sure it perfectly suits your level of expertise and needs. If you bought this book already, then congratulations! You are now well prepared to dig deeper into the most popular web framework for the fastest-growing platform. he demand for skills in Node.js and Express.js is on the verge of rapid growth as both startups and big corporations are gradually becoming aware of the beneits of adopting Node.js. here’s always a gap between early adopters of new technology and mainstream adoption, and mainstream adoption of Node.js is rapidly approaching. For you, as a developer, that means right now is the best time to become proicient with Node.js. Your skills will be in high demand! Why This Book Was Written I started writing about Express.js a few years ago when I wrote Express.js Guide. Self-published in 2013, it was one of the irst books solely dedicated to Express.js, which is the most popular Node.js web framework yet (as of this writing, December 2014). At the time I wrote Express.js Guide, the Express.js oicial web site (http://expressjs.com) ofered only bits of insights, and those were intended for advanced Node.js programmers. I had encountered many people— including through the Hack Reactor program and in my Node.js classes at General Assembly and pariSOMA—who were interested in a deinitive manual, one that would cover how all the diferent components of Express.js work together in real life. he goal of Express.js Guide was to be that resource. After Express.js Guide became the Amazon.com #1 Best Seller in its category, Apress approached me to write this book to expand upon the material and subject it to formal review by professional technical editors. Pro Express.js is much more than a revision or an update of Express.js Guide. It’s a complete remake, with the addition of comments, descriptions, examples, and extras. Pro Express.js has better-reviewed code and text, and up-to-date versions of the libraries (e.g., Express.js v4.8.1). Many things have changed between writing the two books. Node.js was forked at io.js. TJ Holowaychuk, the creator of Express.js, stopped being actively involved with Node.js, and now StrongLoop (http:/strongloop.com) maintains the framework’s repository. he development on Express.js is as rapid as ever. It’s more stable and more secure. And I see nothing but a brighter future for Express.js and Node.js! Who Should Own This Book his book is intended for software engineers and web developers who are already luent in programming and front- end JavaScript. To get the most beneit from Pro Express.js, you should be familiar with basic Node.js concepts, such as process and global, and should also know core modules, including stream, cluster, and bufer. If you’re thinking about starting a Node.js project or rewriting an existing one, and your weapon of choice is Express.js, this guide is for you! It will answer most of your “how” and “why” questions. xxiii www.allitebooks.com ■ INTRODUCTION What This Book Is Pro Express.js is an exhaustive book on one particular library. Unlike Practical Node.js (Apress, 2014) which covered many libraries, Pro Express.js is focused on the single module Express.js. Of course, where it’s necessary to cover other related libraries, such as middleware, the book touches on those as well, but not as extensively as on the framework itself. Pro Express.js covers middleware, the command-line interface and scafolding, rendering templates, extracting params from dynamic URLs, parsing payloads and cookies, managing authentication with sessions, error handling, and prepping apps for production. Pro Express.js is organized into four distinct parts, the individual chapters of which are set forth in the Table of Contents: 1. Getting Started: A hands-on, quick-start walkthrough to give you a taste of the framework 2. Deep API Reference: Serves as an Express.js v4.8.1 API reference that you can read in full or skim through as you need information about certain methods 3. Solving Common and Abstract Problems: The best practices for code organization and patterns—topics that are good to know when working with Express.js 4. Tutorials and Examples: Tutorials (meticulously depicted coding exercises) and examples (less detailed explanations of more complex apps) for the real world What This Book Is Not his book is not an introduction to Node.js, nor is it a book that covers all aspects of building a modern-day web application in great detail, such as WebSockets, databases, and (of course) front-end development. You won’t ind aids for learning programming or JavaScript fundamentals here, because this is not a beginners’ book. For an introduction to Node.js, MongoDB, and front-end development with Backbone.js, you might want to take a look at my book Rapid Prototyping with JS: Agile JavaScript Development (http://rapidprototypingwithjs.com)