Logout succeed
Logout succeed. See you again!

Developing Microservices with Node.js PDF
Preview Developing Microservices with Node.js
[ 1 ] Developing Microservices with Node.js Learn to develop efficient and scalable microservices for server-side programming in Node.js using this hands-on guide David Gonzalez BIRMINGHAM - MUMBAI Developing Microservices with Node.js Copyright © 2016 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: April 2016 Production reference: 1220416 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-740-6 www.packtpub.com Credits Author Project Coordinator David Gonzalez Judie Jose Reviewer Proofreader Kishore Kumar Yekkanti Safis Editing Commissioning Editor Indexer Veena Pagare Monica Ajmera Mehta Acquisition Editor Production Coordinator Divya Poojari Arvindkumar Gupta Content Development Editor Cover Work Abhishek Jadhav Arvindkumar Gupta Technical Editor Pranil Pathare Copy Editor Vibha Shukla About the Author David Gonzalez is a language-agnostic software engineer working in financial services for a number of years, trying to find solutions for the right level of abstraction and learning how to get the right balance between too concrete and too abstract. He studied in Spain, but soon moved to the wider and more interesting market of Dublin, where he has been living since 2011. David is currently working as an independent consultant in the FinTech sector. The URL to his Linkedin account is https://ie.linkedin.com/in/david-gonzalez-737b7383. He loves experimenting with new technologies and paradigms in order to get the broader picture of the complex world of software development. To my wife, Ester, thanks for your unconditional support in every single aspect of my life. To my unborn daughter, Elena, may the life give you all the happiness that you are bringing to your parents. About the Reviewer Kishore Kumar Yekkanti is an experienced professional who has worked across various domains and technologies over the past ten years. He is passionate about eliminating the waste during the software development. Kishore is a huge contributor to and follower of agile principles. He is a full-stack developer who wants to build the end-to-end systems, and a polyglot programmer. His current focus is on scaling microservices in highly distributed applications that are deployed using container- based systems (Docker) on cloud. He has worked as the lead/principal engineer for many well-known companies such as Thoughtworks, CurrencyFair, and others, where he lead the teams to attain nirvana through microservices. For my partner and best friend Jyothsna, and my daughter, Dhruti, who continues to humour me despite of my crazy schedules. www.PacktPub.com eBooks, discount offers, and more 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 https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read 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 a web browser Table of Contents Preface vii Chapter 1: Microservices Architecture 1 Need for microservices 1 Monolithic software 2 Microservices in the real world 2 Microservice-oriented architectures 3 How is it better? 3 Shortcomings 3 Key design principles 4 Business units, no components 5 Smart services, dumb communication pipes 8 Decentralization 9 Technology alignment 12 How small is too small? 12 Key benefits 13 Resilience 13 Scalability 14 Technology heterogeneity 15 Replaceability 17 Independence 18 Why is replaceability important? 18 Easy to deploy 18 SOA versus microservices 19 Why Node.js? 20 API aggregation 21 The future of Node.js 22 Summary 23 [ i ] Table of Contents Chapter 2: Microservices in Node.js – Seneca and PM2 Alternatives 25 Need for Node.js 25 Installing Node.js, npm, Seneca, and PM2 26 Learning npm 27 Our first program – Hello World 29 Node.js threading model 31 Modular organization best practices 31 Javascript 32 SOLID design principles 33 Seneca – a microservices framework 36 Inversion of control done right 39 Pattern matching in Seneca 40 Patrun – a pattern-matching library 41 Reusing patterns 42 Writing plugins 43 Web server integration 47 PM2 – a task runner for Node.js 51 Single-threaded applications and exceptions 51 Using PM2 – the industry-standard task runner 53 Summary 58 Chapter 3: From the Monolith to Microservices 59 First, there was the monolith 60 How to tackle organic growth? 60 How abstract is too abstract? 63 Then the microservices appeared 64 Disadvantages 71 Splitting the monolith 71 Problems splitting the monolith – it is all about the data 72 Organizational alignment 73 Summary 75 Chapter 4: Writing Your First Microservice in Node.js 77 Micromerce – the big picture 77 Product Manager – the two-faced core 79 Fetching products 81 Fetching by category 82 Fetching by ID 82 Adding a product 83 Removing a product 84 Editing a product 84 [ ii ] Table of Contents Wiring everything up 85 Integrating with Express – how to create a REST API 89 The e-mailer – a common problem 90 How to send e-mails 91 Defining the interface 91 Setting up Mandrill 92 Hands on – integrating Mandrill in your microservice 94 The fallback strategy 99 The order manager 101 Defining the microservice – how to gather non-local data 101 The order manager – the code 105 Calling remote services 105 Resilience over perfection 107 The UI – API aggregation 108 Need for frontend microservice 109 The code 109 Service degradation – when the failure is not a disaster 117 Circuit breakers 118 Seneca – a simple puzzle that makes our lives easier 120 Seneca and promises 122 Debugging 126 Summary 129 Chapter 5: Security and Traceability 131 Infrastructure logical security 131 SSH – encrypting the communications 132 Application security 135 Common threats – how to be up to date 135 Injection 136 Cross-site scripting 139 Cross-site request forgery 141 Open redirects 143 Effective code reviews 144 Traceability 145 Logging 145 Tracing requests 147 Auditing 148 HTTP codes 149 1xx – informational 150 2xx – success codes 150 3xx – redirection 150 4xx – client errors 151 [ iii ]