Pro REST API Development with Node.js.pdf - Pro REST API Development with Node.js Fernando Doglio Pro REST API Development with Node.js Fernando Doglio

Pro REST API Development with Node.js.pdf - Pro REST API...

This preview shows page 1 out of 191 pages.

Unformatted text preview: Pro REST API Development with Node.js Fernando Doglio Pro REST API Development with Node.js Fernando Doglio La Paz, Canelones Uruguay ISBN-13 (pbk): 978-1-4842-0918-9 DOI 10.1007/978-1-4842-0917-2 ISBN-13 (electronic): 978-1-4842-0917-2 Library of Congress Control Number: 2015941272 Copyright © 2015 by Fernando Doglio This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Louise Corrigan Technical Reviewer: Jose Dieguez Castro Editorial Board: Steve Anglin, Mark Beckner, Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Matt Wade, Steve Weiss Coordinating Editor: Christine Ricketts Copy Editor: Kimberly Burton-Weisman Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at . Any source code or other supplementary material referenced by the author in this text is available to readers at . For detailed information about how to locate your book’s source code, go to . Printed on acid-free paper To my loving wife, without whom this book would’ve never happened… Thank you! Contents at a Glance About the Author ..................................................................................................... xi About the Technical Reviewer ............................................................................... xiii Acknowledgments ...................................................................................................xv Introduction ...........................................................................................................xvii ■Chapter 1: Rest 101 ............................................................................................... 1 ■Chapter 2: API Design Best Practices .................................................................. 25 ■Chapter 3: Node.js and REST ............................................................................... 47 ■Chapter 4: Architecting a REST API ..................................................................... 65 ■Chapter 5: Working with Modules ....................................................................... 79 ■Chapter 6: Planning Your REST API ................................................................... 111 ■Chapter 7: Developing Your REST API ................................................................ 123 ■Chapter 8: Troubleshooting ............................................................................... 167 Index ..................................................................................................................... 175 v Contents About the Author ..................................................................................................... xi About the Technical Reviewer ............................................................................... xiii Acknowledgments ...................................................................................................xv Introduction ...........................................................................................................xvii ■Chapter 1: Rest 101 ............................................................................................... 1 Where Did It All Start? ...................................................................................................... 1 REST Constraints .............................................................................................................. 3 Client-Server .......................................................................................................................................... 3 Stateless ................................................................................................................................................. 3 Cacheable ............................................................................................................................................... 4 Uniform Interface.................................................................................................................................... 5 Layered System ...................................................................................................................................... 6 Code-on-Demand ................................................................................................................................... 7 Resources, Resources, Resources ................................................................................... 8 Representations...................................................................................................................................... 9 Resource Identifier ............................................................................................................................... 10 Actions .................................................................................................................................................. 11 Hypermedia in the Response and Main Entry Point ............................................................................. 12 Status Codes .................................................................................................................. 16 REST vs. the Past ........................................................................................................... 18 Summary ........................................................................................................................ 24 vii ■ CONTENTS ■Chapter 2: API Design Best Practices .................................................................. 25 What Defines a Good API? .............................................................................................. 25 Developer Friendly ......................................................................................................... 25 Communication’s Protocol .................................................................................................................... 26 Easy-to-Remember Access Points ....................................................................................................... 26 Uniform Interface.................................................................................................................................. 27 Extensibility .................................................................................................................... 29 How Is Extensibility Managed? ............................................................................................................. 30 Up-to-Date Documentation ............................................................................................ 32 Proper Error Handling ..................................................................................................... 34 Phase 1: Development of the Client ...................................................................................................... 34 Phase 2: The Client Is Implemented and Being Used by End Users ...................................................... 36 Multiple SDK/Libraries.................................................................................................... 36 Security .......................................................................................................................... 37 Accessing the System .......................................................................................................................... 37 Scalability ....................................................................................................................... 42 Summary ........................................................................................................................ 45 ■Chapter 3: Node.js and REST ............................................................................... 47 Asynchronous Programming .......................................................................................... 48 Async Advanced.................................................................................................................................... 51 Asynchronous I/O ........................................................................................................... 54 Async I/O vs. Sync I/O ........................................................................................................................... 54 Simplicity........................................................................................................................ 56 Dynamic Typing .................................................................................................................................... 56 Object-Oriented Programming Simplified............................................................................................. 57 Prototypal Inheritance .......................................................................................................................... 58 Functional Programming Support......................................................................................................... 59 Duck Typing .......................................................................................................................................... 60 Native Support for JSON ....................................................................................................................... 60 viii ■ CONTENTS npm: The Node Package Manager.................................................................................. 61 Who’s Using Node.js? ..................................................................................................... 63 Summary ........................................................................................................................ 63 ■Chapter 4: Architecting a REST API ..................................................................... 65 The Request Handler, the Pre-Process Chain, and the Routes Handler .................................................................................................. 66 MVC: a.k.a. Model–View–Controller ............................................................................... 69 Alternatives to MVC .............................................................................................................................. 72 Response Handler .......................................................................................................... 76 Summary ........................................................................................................................ 78 ■Chapter 5: Working with Modules ....................................................................... 79 Our Alternatives .............................................................................................................. 79 Request/Response Handling................................................................................................................. 79 Routes Handling ................................................................................................................................... 80 Middleware ........................................................................................................................................... 80 Up-to-Date Documentation ................................................................................................................... 81 Hypermedia on the Response ............................................................................................................... 81 Response and Request validation......................................................................................................... 81 The List of Modules .............................................................................................................................. 82 Summary ...................................................................................................................... 109 ■Chapter 6: Planning Your REST API ................................................................... 111 The Problem ................................................................................................................. 111 The Specifications .............................................................................................................................. 113 Choosing the Right Modules for the Job............................................................................................. 120 Summary ...................................................................................................................... 121 ix ■ CONTENTS ■Chapter 7: Developing Your REST API ................................................................ 123 Minor Changes to the Plan ........................................................................................... 123 Simplification of the Store: Employee Relationship ............................................................................ 124 Adding Swagger UI ............................................................................................................................. 124 Simplified Security ............................................................................................................................. 124 A Small Backdoor for Swagger ........................................................................................................... 124 MVC .................................................................................................................................................... 125 Folder Structure ........................................................................................................... 125 The Source Code .......................................................................................................... 126 controllers .......................................................................................................................................... 126 lib ........................................................................................................................................................ 146 models ................................................................................................................................................ 151 request_schemas ............................................................................................................................... 156 schemas ............................................................................................................................................. 157 swagger-ui.......................................................................................................................................... 163 Root Folder ......................................................................................................................................... 163 Summary ...................................................................................................................... 166 ■Chapter 8: Troubleshooting ............................................................................... 167 Asynchronous Programming ........................................................................................ 167 The Controllers Action’s Code ............................................................................................................. 168 The Middleware Functions ................................................................................................................. 169 Issues Configuring the Swagger UI .............................................................................. 170 CORS: a.k.a. Cross-Origin Resource Sharing................................................................ 171 Data Types .................................................................................................................... 171 Summary ...................................................................................................................... 173 Index ..................................................................................................................... 175 x About the Author Fernando Doglio has worked as a web developer for the past 10 years. In that time, he has come to love the Web, and has had the opportunity to work with most leading technologies, such as PHP, Ruby on Rails, MySQL, Node. js, Angular.js, AJAX, REST APIs, and others. In his spare time, Fernando likes to tinker and learn new things, which is why his GitHub account keeps getting new repos every month. He’s also a big open source supporter, trying to bring new people to it with the help of the site he created at lookingforpullrequests.com. When not programming, he is spending time with his family. Fernando can be contacted on Twitter @deleteman123. xi About the Technical Reviewer Jose Dieguez Castro is a senior system administrator currently employed as a freelance consultant. He has worked on a wide range of projects— from small to large infrastructures, in private to public sectors. When is asked about his specialty, he replies, “Get th...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture