Node Cookbook - Third Edition
eBook - ePub

Node Cookbook - Third Edition

  1. 654 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

About this book

Over 60 high-quality recipes covering debugging, security, performance, microservices, web frameworks, databases, deployment and more; rewritten for Node.js 8, Node.js 6, and Node.js 4About This Book• Actionable recipes across the full spectrum of Node.js development• Cutting edge techniques and tools for measuring and improving performance• Best practices for creating readily-scalable production systemsWho This Book Is ForIf you have good knowledge of JavaScript and want to build fast, efficient, scalable client-server solutions, then this book is for you. Some experience with Node.js is assumed to get the most out of this book. If working from a beginner level Node Cookbook 2nd Edition is recommended as a primer for Node Cookbook 3rd Edition. What You Will Learn• Debug Node.js programs• Write and publish your own Node.js modules• Detailed coverage of Node.js core API's• Use web frameworks such as Express, Hapi and Koa for accelerated web application development• Apply Node.js streams for low-footprint data processing• Fast-track performance knowledge and optimization abilities• Persistence strategies, including database integrations with MongoDB, MySQL/MariaDB, Postgres, Redis, and LevelDB• Apply critical, essential security concepts• Use Node with best-of-breed deployment technologies: Docker, Kubernetes and AWSIn DetailToday's web demands efficient real-time applications and scalability. Asynchronous event-driven programming is ideal for this, and this is where Node.js comes in. Server-side JavaScript has been here since the 90s, but Node got it right.With Node for tooling and server-side logic, and a browser-based client-side UI, everything is JavaScript. This leads to rapid, fluid development cycles. The full-stack, single language experience means less context-switching between languages for developers, architects and whole teams.This book shows you how to build fast, efficient, and scalable client-server solutions using the latest versions of Node.The book begins with debugging tips and tricks of the trade, and how to write your own modules. Then you'll learn the fundamentals of streams in Node.js, discover I/O control, and how to implement the different web protocols.You'll find recipes for integrating databases such as MongoDB, MySQL/MariaDB, Postgres, Redis, and LevelDB. We also cover the options for building web application with Express, Hapi and Koa.You will then learn about security essentials in Node.js and advanced optimization tools and techniques.By the end of the book you will have acquired the level of expertise to build production-ready and scalable Node.js systems.The techniques and skills you will learn in this book are based on the best practices developed by nearForm, one of the leaders in Node implementations, who supported the work of the authors on this book.Style and approachThis recipe-based practical guide presents each topic with step-by-step instructions on how you can create fast and efficient server side applications using the latest features and capabilities in Node 8 whilst also supporting usage with Node 4 and 6.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Node Cookbook - Third Edition by David Mark Clements, Matthias Buus, Matteo Collina, Peter Elger in PDF and/or ePUB format, as well as other popular books in Computer Science & Databases. We have over one million books available in our catalogue for you to explore.

Information

Building Microservice Systems

This chapter covers the following recipes:
  • Creating a simple RESTful microservice
  • Consuming a service
  • Setting up a development environment
  • Standardizing service boilerplate
  • Using containerized infrastructure
  • Service discovery with DNS
  • Adding a Queue based service

Introduction

In recent years, microservices and distributed systems have become increasingly popular.
Not only does breaking a system into small independent processes suit a single-threaded event-loop platform such as Node, but there can be significant advantages in adopting a microservices architecture, such as:
  • Focus: Each service should do one thing only and do it well. This means that an individual microservice should contain a small amount of code that is easy for an individual developer to reason about.
  • Decoupling: Services run in their own process space and are therefore decoupled from the rest of the system. This makes it easy to replace an individual microservice without greatly perturbing the rest of the system.
  • Fine Grained Continuous Delivery/Deployment: Services are individually deployable, this leads to a model whereby deployment can be an ongoing process. thus removing the need for Big Bang deployments.
  • Individually scalable: Systems may be scaled at the service level leading to more efficient use of compute resources.
  • Language independent: Microservice systems may be composed of services written in multiple languages, allowing developers to select the most appropriate tool for each specific job.
Of course it is not always appropriate to use microservices, certainly the golden hammer anti-pattern should be avoided at all costs. However, it is a powerful approach when applied correctly.
In this chapter, we will learn how to construct a simple RESTful microservice as well as how this service might be consumed. We will also look at how to set up a local development environment using the Fuge toolkit. Then we'll advance to building services that communicate over protocols other than simple HTTP. Finally, we will create a simple service discovery mechanism to allow us to consume our services without hard coding system configuration into each service.
Before diving in, let's take a brief moment to review our definition of a microservice and how this concept plays into a reference architectural frame.
The following figure depicts a typical microservice system:
Our reference architecture contains the following elements that are typical to most microservice style systems:
  • Clients: Typically, web-based or mobile applications, make HTTP connections to an API layer.
  • Static assets: Such as images, style sheets, and other elements that are used to render the user interface.
  • API layer: This is usually a thin layer that provides the routing between client requests and microservices that ultimately respond to these requests.
  • Service Discovery: A mechanism for discovering and routing to microservices. This can be as simple as a shared configuration file or a more dynamic mechanism such as DNS.
  • Direct response services: These types of services are typically reached via a point to point protocol such as HTTP or raw TCP and will usually perform a distinct action and return a result.
  • Async services: These types of services are typically invoked via a bus-based technology such as RabbitMQ or Apache Kafka. These may or may not return a response to the caller.
  • Data sources and External APIs: Services will usually interact with one or more data sources or external systems in order to generate responses to requests.
Based on this logical architecture we will use the following definition for a microservice:
A microservice is a small, highly cohesive unit of code that has responsibility for a small functional area of a system. It should be independently deployable and should be of a size that it could be rewritten by a single developer in two weeks at maximum.
To break this down further, a microservice has the following qualities:
  • Limited, focused responsibility
  • Highly cohesive, tightly scoped functionality
  • Independently deployable
  • Small enough to be rewritten by a single developer in under two weeks
In the following recipes, we will look at how microservices operate in the context of an example system, how to set up an effective development environment for this style of coding, and also look microservice messaging and communication protocols.
This chapter concerns itself more with architectural structure than implementation details. Explanations tend to focus on how the pieces of a system integrate at an external, higher level rather than detailing in depth the intricacies of internal logic. However, much of the code, abstractions, patterns, and techniques used here are covered throughout the rest of this book.

Creating a simple RESTful microservice

In this recipe, we will build a simple microservice using the restify module. Restify is an easy to use middleware-centric framework (similar in API to Express) that is designed to help us rapidly build services that can be consumed over HTTP. Once we have built our first service we will test our service using the curl command.

Getting ready

To get going let's create a fresh empty directory that we'll call it micro:
$ mkdir micro
$ cd micro

How to do it...

We're going to create a service that adds two numbers together.
A service is simply a Node process, so let's go ahead and create an adderservice folder inside our micro directory, initialize our new folder as a package, a...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. Foreword
  5. About the Authors
  6. About the Reviewers
  7. www.PacktPub.com
  8. Customer Feedback
  9. About nearForm
  10. Preface
  11. Debugging process*
  12. Writing Modules
  13. Coordinating I/O
  14. Using Streams
  15. Wielding Web Protocols
  16. Persisting to Databases
  17. Working with Web Frameworks
  18. Dealing with Security
  19. Optimizing Performance
  20. Building Microservice Systems
  21. Deploying Node.js