www.it-ebooks.info Mastering Web Application Development with Express A comprehensive guide to developing production-ready web applications with Express Alexandru Vlăduțu BIRMINGHAM - MUMBAI www.it-ebooks.info Mastering Web Application Development with Express Copyright © 2014 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: September 2014 Production reference: 1180914 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-108-3 www.packtpub.com Cover image by Goldie Jason ([email protected]) www.it-ebooks.info Credits Author Project Coordinator Alexandru Vlăduțu Swati Kumari Reviewers Proofreaders Johan Borestad Ameesha Green Mohit Goenka Maria Gould Arjunkumar Krishnamoorthy Paul Hindle Dave Poon Jonathan Todd Commissioning Editor Indexers Ashwin Nair Rekha Nair Priya Sane Acquisition Editor James Jones Graphics Content Development Editors Abhinash Sahu Nadeem N. Bagban Poonam Jain Production Coordinator Conidon Miranda Technical Editors Novina Kewalramani Cover Work Pratik More Conidon Miranda Copy Editors Mradula Hegde Dipti Kapadia Insiya Morbiwala Alfida Paiva Stuti Srivastava www.it-ebooks.info About the Author Alexandru Vlăduțu is a full-time JavaScript developer based in Bucharest, Romania. He started creating applications with PHP about 5 years ago, but after finding out about server-side JavaScript with Node.js, he has never had to switch technologies again. You may have seen him answering questions on Stack Overflow under the nickname alessioalex, where he is among the top three overall answerers for tags such as Node.js, Express, Mongoose, and Socket.IO. By day, he battles cross-browser compatibility issues, but by night, he brings together embedded databases, servers, and caching layers in single applications using the good parts of JavaScript. Apart from the geeky stuff, he enjoys spending time with his wife. The first time I saw the video of Ryan Dahl presenting Node at JS Conf 2009, I was amazed. I have been fanatically working with Node ever since, and Ryan deserves credit for this. I would like to thank TJ Holowaychuk for authoring Express, and the Node community for being friendly, helpful, and extremely active. While writing this book, I had invaluable feedback from the reviewers as well as the Packt Publishing team; so thanks a lot everybody! Most importantly, I would like to thank my wife, Diana, for her support, encouragement, and patience. www.it-ebooks.info About the Reviewers Johan Borestad lives and works in Stockholm, Sweden. With 10 years of experience in several successful start-ups, he has built up a deep knowledge of the industry. As a very outgoing and pragmatic perfectionist, he is constantly seeking new ways to improve himself and his team members. While always striving to deliver world-class products, Johan also enjoys telling bad jokes and drinking way too much coffee. He is currently working at Klarna, building the Klarna Checkout. It is a multimarket, single-page application that is revolutionizing the e-commerce business currently. Its strong focus on usability and simplifying the buying process has made it a huge success in the Nordics and Germany. He has previously also reviewed Express Web Application Development, Packt Publishing. I'd like to give my warmest thank-you to my lovely family as well as to Klarna and my teammates who helped me during tough times. www.it-ebooks.info Mohit Goenka is a Software Developer in the Yahoo! Mail team. He graduated from the University of Southern California (USC) with a Master of Science degree in Computer Science. His thesis emphasized game theory and human behavior concepts as applied in real-world security games. He also received an award for academic excellence from the Office of International Services at the University of Southern California. He has showcased his presence in various realms of computers, including artificial intelligence, machine learning, path planning, multiagent systems, neural networks, computer vision, computer networks, and operating systems. During his tenure as a student, Mohit won multiple competitions, cracked codes, and presented his work on the Detection of Untouched UFOs to a wide range of audiences. Not only is he a software developer by profession but coding is also his hobby. He spends most of his spare time learning about emerging trends and grooming his technical skills. What adds a feather to his cap are Mohit's poetic skills. Some of his poems are part of the University of Southern California Libraries archive under the cover of The Lewis Carroll Collection. In addition to this, he has made significant contributions by volunteering his time to serve the community. Arjunkumar Krishnamoorthy is a Principal Engineer with Causeway Technologies in Bengaluru, India. He is well-versed in Java, JavaScript, Node.js, and Angular.js, among others. He has contributed to open source projects. He is passionate about programming, research, and open source technologies. www.it-ebooks.info Dave Poon is a UX/UI designer, web developer, and entrepreneur based in Sydney. He started his career as a freelance graphic designer and web designer in 1998 and worked with web development agencies and medium-size enterprises. After graduating from Central Queensland University with a degree in Multimedia Studies and a Master's degree in IT, he began his love affair with Drupal and works for a variety of companies that use Drupal. Now, he is evangelizing good user experience and interaction design practices to start-ups and enterprises. Currently, he is a Design Lead at Suncorp, one of the biggest financial institutions in Australia. He is also the cofounder of Erlango (http://erlango.com), a digital product development and design start-up, located in Sydney and Hong Kong, that creates user-centered digital products and tools for designers and users. He is the author of Drupal 7 Fields/CCK Beginner's Guide, Packt Publishing. He is also the technical reviewer of Drupal Intranets with Open Atrium, Tracy Smith, Packt Publishing, and Advanced Express Web Application Development, Andrew Keig, Packt Publishing. I would like to thank my wife, Rita, for her endless patience and support. Without her, whatever I do would be meaningless. I would also like to thank my father for his continued encouragement. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Diving into Express 7 The best parts of Express 7 Comparing Express with other frameworks 8 Goal 8 Conventions 9 Databases 9 Views 9 Overall 9 Use cases 10 Complex applications with heavy I/O bound operations 10 Single-page applications 10 Reusable applications 11 Code sharing between the server and the client 11 A base to create more complex frameworks 11 Bad use cases 12 Express into the wild 12 The application structure 13 Group files by features 13 Model-View-Controller 14 Developing a real MVC application 15 Summary 32 Chapter 2: Component Modularity Using Middleware 33 Connecting middleware 33 The functionality of middleware 34 Pushing items to an array 36 Looking at the execution flow using logs 36 www.it-ebooks.info