Node.js Complete Reference Guide - Valentin Bojinov - E-Book

Node.js Complete Reference Guide E-Book

Valentin Bojinov

0,0
41,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Node.js Complete Reference Guide is your one-stop solution to building highly scalable, single-language applications that share code between the server and client

Key Features

  • Explore the latest JavaScript features and EcmaScript modules
  • Walk through different stages of developing robust applications using Node.js 10
  • Create rich and scalable RESTful API solutions from scratch

Book Description

Node.js Complete Reference Guide takes you through rudimentary knowledge of JavaScript and server-side development to create, maintain, deploy, and test your own Node.js applications.

You will begin by learning how to use the HTTP Server and Client objects, store data with both SQL and MongoDB databases, and unit test applications with Mocha 5.x, and functionally test them with Puppeteer 1.1.x. Then, you will learn to create scalable and rich RESTful applications on the Node.js platform and write a simple HTTP request handler with self-descriptive URLs. You will learn to set accurate HTTP status codes, study how to keep your applications backward-compatible and explore some authentication techniques to secure your application. Then, you will study how Node.js has emerged as a strong candidate for developing microservices.

With this Learning Path, you will be able to use the best practices and create efficient microservices.

This Learning Path includes content from the following Packt products:

  • RESTful Web API Design with Node.js 10, Third Edition by Valentin Bojinov
  • Node.js Web Development, Fourth Edition by David Herron
  • Hands-On Microservices with Node.js by Diogo Resende

What you will learn

  • Work with REST service development using the Restify framework
  • Use data storage engines, such as MySQL, SQLITE3, and MongoDB in apps
  • Apply user authentication methods with OAuth2
  • Perform real-time communication with the front-end using Socket.IO
  • Implement Docker microservices in development, testing, and deployment
  • Perform unit testing with Mocha 5.x and functional testing with Puppeteer 1.1.x
  • Use self-descriptive URLs and set accurate HTTP status codes
  • Deploy a cloud-native microservice to an online provider

Who this book is for

Node.js Complete Reference Guide is designed for web developers who have a rudimentary understanding of JavaScript and web application development, are keen to enrich their development skills to create RESTful applications, and want utilize their skills to build microservices.

Valentin Bojinov studied computer science at the Technological School of Electronic Systems in Sofia, Bulgaria, a college within the Technical University of Sofia. He holds a B.Sc. in telecommunication and information engineering. Valentin is an expert in Java, SOAP, RESTful web services, and B2B integration. He specializes B2B Integration and Service Oriented Architecture and currently works as an Senior Integration Consultant in an UK consultancy company Estafet Limited. David Herron is a software engineer in Silicon Valley, working on projects from an X.400 e-mail server to assist launching the OpenJDK project, to Yahoo's Node.js application-hosting platform, and applications to monitor solar power array performance. David writes about electric vehicles, environmental issues, and green technology on LongTailPipe, and about other topics, including Node.js, Drupal, and Doctor Who on TechSparx. Using Node.js he developed AkashaCMS. Diogo Resende is a developer with more than 15 years of experience, working with Node.js almost from the beginning. His computer education and experience in many industry and telecommunication projects have given him a wider background knowledge of other architecture components that influence the overall performance.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 768

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Node.js Complete Reference Guide

 

 

 

 

 

Discover a more sustainable way of writing software with high levels of reusability and collaboration using Node.js

 

 

 

 

 

 

 

 

Valentin Bojinov
David Herron
Diogo Resende

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Node.js Complete Reference Guide

Copyright © 2018 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 authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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: December 2018

Production reference: 1181218

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78995-211-7

www.packtpub.com

 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

Packt.com

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.packt.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.packt.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. 

Contributors

About the authors

Valentin Bojinov studied computer science at the Technological School of Electronic Systems in Sofia, Bulgaria, a college within the Technical University of Sofia. He holds a B.Sc. in telecommunication and information engineering. Valentin is an expert in Java, SOAP, RESTful web services, and B2B integration. He specializes B2B Integration and Service Oriented Architecture and currently works as an Senior Integration Consultant in an UK consultancy company Estafet Limited.

David Herron is a software engineer in Silicon Valley, working on projects from an X.400 e-mail server to assist launching the OpenJDK project, to Yahoo's Node.js application-hosting platform, and applications to monitor solar power array performance. David writes about electric vehicles, environmental issues, and green technology on LongTailPipe, and about other topics, including Node.js, Drupal, and Doctor Who on TechSparx. Using Node.js he developed AkashaCMS.

 

 

Diogo Resende is a developer with more than 15 years of experience, working with Node.js almost from the beginning. His computer education and experience in many industry and telecommunication projects have given him a wider background knowledge of other architecture components that influence the overall performance.

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Node.js Complete Reference Guide

About Packt

Why subscribe?

Packt.com

Contributors

About the authors

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Conventions used

Get in touch

Reviews

About Node.js

The capabilities of Node.js

Server-side JavaScript

Why should you use Node.js?

Popularity

JavaScript at all levels of the stack

Leveraging Google's investment in V8

Leaner, asynchronous, event-driven model

Microservice architecture

Node.js is stronger for having survived a major schism and hostile fork

Threaded versus event-driven architecture

Performance and utilization

Is Node.js a cancerous scalability disaster?

Server utilization, the business bottom line, and green web hosting

Embracing advances in the JavaScript language

Deploying ES2015/2016/2017/2018 JavaScript code

Node.js, the microservice architecture, and easily testable systems

Node.js and the Twelve-Factor app model

Summary

Setting up Node.js

System requirements

Installing Node.js using package managers

Installing on macOS with MacPorts

Installing on macOS with Homebrew

Installing on Linux, *BSD, or Windows from package management systems

Installing Node.js in the Windows Subsystem for Linux (WSL)

Opening an administrator-privileged PowerShell on Windows

Installing the Node.js distribution from nodejs.org

Installing from source on POSIX-like systems

Installing prerequisites

Installing developer tools on macOS

Installing from source for all POSIX-like systems

Installing from source on Windows

Installing multiple Node.js instances with nvm

Installing nvm on Windows

Native code modules and node-gyp

Node.js versions policy and what to use

Editors and debuggers

Running and testing commands

Node.js's command-line tools

Running a simple script with Node.js

Conversion to async functions and the Promise paradigm

Launching a server with Node.js

NPM – the Node.js package manager

Node.js, ECMAScript 2015/2016/2017, and beyond 

Using Babel to use experimental JavaScript features

Summary

Node.js Modules

Defining a module

CommonJS and ES2015 module formats

CommonJS/Node.js module format

ES6 module format

JSON modules

Supporting ES6 modules on older Node.js versions

Demonstrating module-level encapsulation

Finding and loading CommonJS and JSON modules using require

File modules

Modules baked into Node.js binary

Directories as modules

Module identifiers and pathnames

An example of application directory structure

Finding and loading ES6 modules using import

Hybrid CommonJS/Node.js/ES6 module scenarios

Dynamic imports with import()

The import.meta feature

npm - the Node.js package management system

The npm package format

Finding npm packages

Other npm commands

Installing an npm package

Installing a package by version number

Global package installs

Avoiding global module installation

Maintaining package dependencies with npm

Automatically updating package.json dependencies

Fixing bugs by updating package dependencies

Packages that install commands

Configuring the PATH variable to handle commands installed by modules

Configuring the PATH variable on Windows

Avoiding modifications to the PATH variable

Updating outdated packages you've installed

Installing packages from outside the npm repository

Initializing a new npm package

Declaring Node.js version compatibility

Publishing an npm package

Explicitly specifying package dependency version numbers

The Yarn package management system

Summary

HTTP Servers and Clients

Sending and receiving events with EventEmitters

JavaScript classes and class inheritance

The EventEmitter Class

The EventEmitter theory

HTTP server applications

ES2015 multiline and template strings

HTTP Sniffer – listening to the HTTP conversation

Web application frameworks

Getting started with Express

Setting environment variables in Windows cmd.exe command line

Walking through the default Express application

The Express middleware

Middleware and request paths

Error handling

Calculating the Fibonacci sequence with an Express application

Computationally intensive code and the Node.js event loop

Algorithmic refactoring

Making HTTP Client requests

Calling a REST backend service from an Express application

Implementing a simple REST server with Express

Refactoring the Fibonacci application for REST

Some RESTful modules and frameworks

Summary

Your First Express Application

Promises, async functions, and Express router functions

Promises and error handling

Flattening our asynchronous code

Promises and generators birthed async functions

Express and the MVC paradigm

Creating the Notes application

Your first Notes model

Understanding ES-2015 class definitions

Filling out the in-memory Notes model

The Notes home page

Adding a new note – create

Viewing notes – read

Editing an existing note – update

Deleting notes – destroy

Theming your Express application

Scaling up – running multiple Notes instances

Summary

Implementing the Mobile-First Paradigm

Problem – the Notes app isn't mobile friendly

Mobile-first paradigm

Using Twitter Bootstrap on the Notes application

Setting it up

Adding Bootstrap to application templates

Alternative layout frameworks

Flexbox and CSS Grids

Mobile-first design for the Notes application

Laying the Bootstrap grid foundation

Responsive page structure for the Notes application

Using icon libraries and improving visual appeal

Responsive page header navigation bar

Improving the Notes list on the front page

Cleaning up the Note viewing experience

Cleaning up the add/edit note form

Cleaning up the delete-note window

Building a customized Bootstrap

Pre-built custom Bootstrap themes

Summary

Data Storage and Retrieval

Data storage and asynchronous code

Logging

Request logging with Morgan

Debugging messages

Capturing stdout and stderr

Uncaught exceptions

Unhandled Promise rejections

Using the ES6 module format

Rewriting app.js as an ES6 module

Rewriting bin/www as an ES6 module

Rewriting models code as ES6 modules

Rewriting router modules as ES6 modules

Storing notes in the filesystem

Dynamic import of ES6 modules

Running the Notes application with filesystem storage

Storing notes with the LevelUP data store

Storing notes in SQL with SQLite3

SQLite3 database schema

SQLite3 model code

Running Notes with SQLite3

Storing notes the ORM way with Sequelize

Sequelize model for the Notes application

Configuring a Sequelize database connection

Running the Notes application with Sequelize

Storing notes in MongoDB

MongoDB model for the Notes application

Running the Notes application with MongoDB

Summary

Multiuser Authentication the Microservice Way

Creating a user information microservice

User information model

A REST server for user information

Scripts to test and administer the user authentication server

Login support for the Notes application

Accessing the user authentication REST API

Login and logout routing functions

Login/logout changes to app.js

Login/logout changes in routes/index.mjs

Login/logout changes required in routes/notes.mjs

View template changes supporting login/logout

Running the Notes application with user authentication

Twitter login support for the Notes application

Registering an application with Twitter

Implementing TwitterStrategy

Securely keeping secrets and passwords

The Notes application stack

Summary

Dynamic Client/Server Interaction with Socket.IO

Introducing Socket.IO

Initializing Socket.IO with Express

Real-time updates on the Notes homepage

The Notes model as an EventEmitter class

Real-time changes in the Notes home page

Changing the homepage and layout templates

Running Notes with real-time homepage updates

Real-time action while viewing notes

Changing the note view template for real-time action

Running Notes with real-time updates while viewing a note

Inter-user chat and commenting for Notes

Data model for storing messages

Adding messages to the Notes router

Changing the note view template for messages

Using a Modal window to compose messages

Sending, displaying, and deleting messages

Running Notes and passing messages

Other applications of Modal windows

Summary

Deploying Node.js Applications

Notes application architecture and deployment considerations

Traditional Linux Node.js service deployment

Prerequisite – provisioning the databases

Installing Node.js on Ubuntu

Setting up Notes and user authentication on the server

Adjusting Twitter authentication to work on the server

Setting up PM2 to manage Node.js processes

Node.js microservice deployment with Docker

Installing Docker on your laptop

Starting Docker with Docker for Windows/macOS

Kicking the tires of Docker

Creating the AuthNet for the user authentication service

MySQL container for Docker

Initializing AuthNet

Script execution on Windows

Linking Docker containers

The db-userauth container

Dockerfile for the authentication service

Configuring the authentication service for Docker

Building and running the authentication service Docker container

Exploring Authnet

Creating FrontNet for the Notes application

MySQL container for the Notes application

Dockerizing the Notes application

Controlling the location of MySQL data volumes

Docker deployment of background services

Deploying to the cloud with Docker compose

Docker compose files

Running the Notes application with Docker compose

Deploying to cloud hosting with Docker compose

Summary

Unit Testing and Functional Testing

Assert – the basis of testing methodologies

Testing a Notes model

Mocha and Chai­ – the chosen test tools

Notes model test suite

Configuring and running tests

More tests for the Notes model

Testing database models

Using Docker to manage test infrastructure

Docker Compose to orchestrate test infrastructure

Executing tests under Docker Compose

MongoDB setup under Docker and testing Notes against MongoDB

Testing REST backend services

Automating test results reporting

Frontend headless browser testing with Puppeteer

Setting up Puppeteer

Improving testability in the Notes UI

Puppeteer test script for Notes

Running the login scenario

The Add Note scenario

Mitigating/preventing spurious test errors in Puppeteer scripts

Configuring timeouts

Tracing events on the Page and the Puppeteer instance

Inserting pauses

Avoiding WebSockets conflicts

Taking screenshots

Summary

REST – What You Did Not Know

REST fundamentals

Principle 1 – Everything is a resource

Principle 2 – Each resource is identifiable by a unique identifier

Principle 3 – Manipulate resources via standard HTTP methods

Principle 4 – Resources can have multiple representations

Principle 5 – Communicate with resources in a stateless manner

The REST goals

Separation of the representation and the resource

Visibility

Reliability

Scalability and performance

Working with WADL

Documenting RESTful APIs with Swagger

Taking advantage of the existing infrastructure

Summary

Building a Typical Web API

Specifying the API

Implementing routes

Querying the API using test data

Content negotiation

API versioning

Self-test questions

Summary

Using NoSQL Databases

MongoDB – a document store database

Database modeling with Mongoose

Testing a Mongoose model with Mocha

Creating a user-defined model around a Mongoose model

Wiring up a NoSQL database module to Express

Self-test questions

Summary

Restful API Design Guidelines

Endpoint URLs and HTTP status codes best practices

Extensibility and versioning

Linked data

Summary

Implementing a Full Fledged RESTful Service

Working with arbitrary data

Linking

Implementing paging and filtering

Caching

Supplying the Cache-Control header in Express applications

Discovering and exploring RESTful services

Summary

Consuming a RESTful API

Consuming RESTful services with jQuery

Troubleshooting and identifying problems on the wire

Cross Origin Resource Sharing

Content Delivery Networks

Handling HTTP status codes on the client side

Summary

Securing the Application

Authentication

Basic authentication

Passport

Passport's basic authentication strategy

Passport's OAuth Strategy

Passport's third-party authentication strategies

Authorization

Transport layer security

Self-test questions

Summary

The Age of Microservices

From monolith to microservices

Patterns of microservices

Decomposable

Autonomous

Scalable

Communicable

Summary

Modules and Toolkits

Seneca

Hydra

Summary

Building a Microservice

Using Hydra

Using Seneca

Plugins

Summary

State

State

Storing state

MySQL

RethinkDB

Redis

Conclusion

Security

Summary

Testing

Integrating tests

Using chai

Adding code coverage

Covering all code

Mocking our services

Summary

Design Patterns

Choosing patterns

Architectural patterns

Front Controller

Layered

Service Locator

Observer

Publish-Subscribe

Using patterns

Planning your microservice

Obstacles when developing

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Machine learning is becoming more and more transformational to businesses every passing day. Machine Learning with Node.js Complete Reference Guide takes you through rudimentary knowledge of JavaScript and server-side development to create, maintain, deploy, and test your own Node.js applications.

You will begin by learning how to use the HTTP Server and Client objects, store data with both SQL and MongoDB databases, and unit test applications with Mocha 5.x, and functionally test them with Puppeteer 1.1.x. Then, you will learn to create scalable and rich RESTful applications on the Node.js platform and write a simple HTTP request handler with self-descriptive URLs. You will learn to set accurate HTTP status codes, study how to keep your applications backward-compatible and explore some authentication techniques to secure your application. Then, you will study how Node.js has emerged as a strong candidate for developing microservices.

With this Learning Path, you will be able to use the best practices and create efficient microservices.

This Learning Path includes content from the following Packt products:

RESTful Web API Design with Node.js 10, Third Edition by Valentin Bojinov

Node.js Web Development, Fourth Edition by David Herron

Hands-On Microservices with Node.js by Diogo Resende

Who this book is for

Node.js Complete Reference Guide is designed for web developers who have a rudimentary understanding of JavaScript and web application development, are keen to enrich their development skills to create RESTful applications, and want utilize their skills to build microservices.

What this book covers

Chapter 1, About Node.js, introduces you to the Node.js platform. It covers its uses, the technological architecture choices in Node.js, its history, the history of server-side JavaScript, why JavaScript should be liberated from the browser, and important recent advances in the JavaScript scene.

Chapter 2, Setting up Node.js, goes over setting up a Node.js developer environment. This includes installing Node.js on Windows, macOS, and Linux. Important tools are covered, including the npm and yarn package management systems and Babel, which is used for transpiling modern JavaScript into a form that's runnable on older JavaScript implementations.

Chapter 3, Node.js Modules, explores the module as the unit of modularity in Node.js applications. We dive deep into understanding and developing Node.js modules and using npm to maintain dependencies. We learn about the new module format, ES6 Modules, that should supplant the CommonJS module format currently used in Node.js, and are natively supported in Node.js 10.x.

Chapter 4, HTTP Servers and Clients, starts exploring web development with Node.js. We develop several small webserver and client applications in Node.js. We use the Fibonacci algorithm to explore the effects of heavy-weight, long-running computations on a Node.js application. We also learn several mitigation strategies, and have our first experience with developing REST services.

Chapter 5, Your First Express Application, begins the section on developing a note-taking application. The first step is getting a basic application running.

Chapter 6, Implementing the Mobile-First Paradigm, uses Bootstrap V4 to implement responsive web design. We take a look at integrating a popular icon set so that we can have pictorial buttons, and go over compiling a custom Bootstrap theme.

Chapter 7, Data Storage and Retrieval, ensures that we don't lose our notes when we restart the application. We explore several database engines and a method to enable easily switching between them at will.

Chapter 8, Multiuser Authentication the Microservice Way, adds user authentication to the note-taking application. Both logged-in and anonymous users can access the application, with varying capabilities based on role. Authentication is supported both for locally stored credentials and for using OAuth against Twitter.

Chapter 9, Dynamic Client/Server Interaction with Socket.IO, lets our users talk with each other in real time. JavaScript code will run in both the browser and the server, with Socket.IO providing the plumbing needed for real-time event exchange. Users will see notes change as they're edited by other users and can leave messages/comments for others.

Chapter 10, Deploying Node.js Applications, helps us understand Node.js application deployment. We look at both traditional Linux service deployment using an /etc/init script and using Docker for both local development and deployment on cloud hosting services.

Chapter 11, Unit Testing and Functional Testing, takes a look at three test development models: unit testing, REST testing, and functional testing. We'll use the popular Mocha and Chai frameworks for the first two, and Puppeteer for the third. Puppeteer uses a headless version of Chrome to support running tests. Docker is used to facilitate setting up and tearing down test environments.

Chapter 12, REST – What You Did Not Know, gives you a brief introduction to the history of REST and how it couples with the HTTP protocol.

Chapter 13, Building a Typical Web API, takes you through structuring your application using human-readable URL and URI parameters. You will get to develop a read-only RESTful service application, using the filesystem for storage.

Chapter 14, Using NoSQL Databases, showcases how to use the MongoDB NoSQL database, and explains the foundation of document data stores.

Chapter 15, Restful API Design Guidelines, explains that there are a number of prerequisites that a RESTful API should meet.

Chapter 16, Implementing a Full Fledged RESTful Service, focuses on implementing a production-ready RESTful service that uses NoSQL to store its data. You will get to learn how to handle binary data and how to version an API while it evolves.

Chapter 17, Consuming a RESTful API, showcases a sample frontend client that serves as a consumption reference implementation.

Chapter 18, Securing the Application, covers restricting access to your data by choosing an appropriate authentication approach. You'll then be able to protect data leakage with transport layer security.

Chapter 19, The Age of Microservices, covers the evolution of computing and how development has changed and shifted from paradigm to paradigm depending on processing capacity and user demand, ultimately resulting in the age of microservices.

Chapter 20, Modules and Toolkits, introduces you to some modules that help you create a microservice, detailing different full toolkits approaches, such as Hydra and Seneca.

Chapter 21, Building a Microservice, covers the development of a simple microservice using Seneca and Hydra.

Chapter 22, State, covers the development of our microservice: from using the server filesystem to moving to a more structured database service, such as MySQL.

Chapter 23, Testing, covers the mocking of services.

Chapter 24, Design Patterns, enumerates some of the most common architectural design patterns and reviews the continuous integration and deployment loop used throughout the book.

To get the most out of this book

The execution of the code examples provided in this Learning Path requires Node.js running on POSIX-like operating systems, various UNIX derivatives (Solaris, for example) or workalikes (Linux, macOS, and so on), as well as on Microsoft Windows. It can run on machines both large and small, including the tiny ARM devices such as the Raspberry Pi microscale embeddable computer for DIY software/hardware projects.

Because many Node.js packages are written in C or C++, you must have a C compiler (such as GCC), Python 2.7 (or later), and the node-gyp package. If you plan to use encryption in your networking code, you will also need the OpenSSL cryptographic library. The modern UNIX derivatives almost certainly come with these, and Node.js's configure script, used when installing from source, will detect their presence. If you need to install them, Python is available at http://python.org and OpenSSL is available at http://openssl.org.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Node.js-Complete-Reference-Guide. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The EventEmitter object is defined in the events module of Node.js."

A block of code is set as follows:

if (anotherNote instanceof Note) { ... it's a Note, so act on it as a Note}

Any command-line input or output is written as follows:

$ npm update express$ npm update

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "If you need something different, click on the DOWNLOADS link in the header for all possible downloads:"

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

About Node.js

Node.js is an exciting new platform for developing web applications, application servers, any sort of network server or client, and general purpose programming. It is designed for extreme scalability in networked applications through an ingenious combination of server-side JavaScript, asynchronous I/O, and asynchronous programming. It is built around JavaScript anonymous functions, and a single execution thread event-driven architecture.

While only a few years old, Node.js has quickly grown in prominence and it's now playing a significant role. Companies, both small and large, are using it for large-scale and small-scale projects. PayPal, for example, has converted many services from Java to Node.js.

The Node.js architecture departs from a typical choice made by other application platforms. Where threads are widely used to scale an application to fill the CPU, Node.js eschews threads because of their inherent complexity. It's claimed that with single-thread event-driven architectures, memory footprint is low, throughput is high, the latency profile under load is better, and the programming model is simpler. The Node.js platform is in a phase of rapid growth, and many are seeing it as a compelling alternative to the traditional web application architectures using Java, PHP, Python, or Ruby on Rails.

At its heart, it is a standalone JavaScript engine with extensions making it suitable for general purpose programming and with a clear focus on application server development. Even though we're comparing Node.js to application server platforms, it is not an application server. Instead, Node.js is a programming run-time akin to Python, Go, or Java SE. While there are web application frameworks and application servers written in Node.js, it is simply a system to execute JavaScript programs.

It is implemented around a non-blocking I/O event loop and a layer of file and network I/O libraries, all built on top of the V8 JavaScript engine (from the Chrome web browser). The rapid performance and feature improvements implemented in Chrome quickly flow through to the Node.js platform. Additionally, a team of folks are working on a Node.js implementation that runs on top of Microsoft's ChakraCore JavaScript engine (from the Edge web browser). That would give the Node.js community greater flexibility by not being reliant on one JavaScript engine provider. Visit https://github.com/nodejs/node-chakracore to take a look at the project.

The Node.js I/O library is general enough to implement any sort of server executing any TCP or UDP protocol, whether it's domain name system (DNS ), HTTP,  internet relay chat (IRC), or FTP. While it supports developing internet servers or clients, its biggest use case is in regular websites, in place of technology such as an Apache/PHP or Rails stack, or to complement existing websites. For example, adding real-time chat or monitoring existing websites can be easily done with the Socket. IO library for Node.js. Its lightweight, high-performance nature often sees Node.js used as a glue service.

A particularly intriguing combination is deploying small services using Docker into cloud hosting infrastructure. A large application can be divided into what's now called microservices that are easily deployed at scale using Docker. The result fits agile project management methods since each microservice can be easily managed by a small team that collaborates at the boundary of their individual API.

This book will give you an introduction to Node.js. We presume the following:

You already know how to write software

You are familiar with JavaScript

You know something about developing web applications in other languages

We will cover the following topics in this chapter:

An introduction to Node.js

Why you should use Node.js

The architecture of Node.js

Performance, utilization, and scalability with Node.js

Node.js, microservice architecture, and testing

Implementing the Twelve-Factor App model with Node.js

We will dive right into developing working applications and recognize that often the best way to learn is by rummaging around in working code.

The capabilities of Node.js

Node.js is a platform for writing JavaScript applications outside web browsers. This is not the JavaScript we are familiar with in web browsers! For example, there is no DOM built into Node.js, nor any other browser capability.

Beyond its native ability to execute JavaScript, the bundled modules provide capabilities of this sort:

Command-line tools (in shell script style)

An interactive-

terminal

 style of program that is 

Read-Eval-Print Loop

(

REPL

)

Excellent process control functions to oversee child processes

A buffer object to deal with binary data

TCP or UDP sockets with comprehensive event-driven callbacks

DNS lookup

An HTTP, HTTPS and HTTP/2 client/server layered on top of the TCP library filesystem access

Built-in rudimentary unit testing support through assertions

The network layer of Node.js is low level while being simple to use. For example, the HTTP modules allow you to write an HTTP server (or client) using a few lines of code. This is powerful, but it puts you, the programmer, very close to the protocol requests and makes you implement precisely those HTTP headers that you should return in request responses.

Typical web application developers don't need to work at a low level of the HTTP or other protocols. Instead, we tend to be more productive, working with higher-level interfaces. For example, PHP coders assume that Apache (or other HTTP servers) is already there providing the HTTP protocol, and that they don't have to implement the HTTP server portion of the stack. By contrast, a Node.js programmer does implement an HTTP server to which their application code is attached.

To simplify the situation, the Node.js community has several web application frameworks, such as Express, providing the higher-level interfaces required by typical programmers. You can quickly configure an HTTP server with baked-in capabilities such as sessions, cookies, serving static files, and logging, letting developers focus on their business logic. Other frameworks provide OAuth 2 support, or focus on REST APIs, and so on.

Node.js is not limited to web service application development. The community around Node.js has taken it in many other directions,

Build tools: Node.js has become a popular choice for developing command-line tools used in software development, or communicating with service infrastructure. Grunt and Gulp are widely used by frontend developers to build assets for websites. Babel is widely used for transpiling modern ES-2016 code to run on older browsers. Popular CSS optimizers and processors, such as PostCSS, are written in Node.js. Static website generation systems such as Metalsmith, Punch, and AkashaCMS, run at the command line and generate website content that you upload to a web server.

Web UI testing: Puppeteer gives you control over a headless-Chrome web browser instance. With it, you can develop Node.js scripts controlling a modern full-featured web browser. Typical use cases involve web scraping and testing web applications.

Desktop applications: Both Electron and node-webkit (NW.js) are frameworks for developing desktop applications for Windows, macOS, and Linux. These frameworks utilize a large chunk of Chrome, wrapped by Node.js libraries, to develop desktop applications using web UI technologies. Applications are written with modern HTML5, CSS3, and JavaScript, and can utilize leading-edge web frameworks, such as Bootstrap, React, or AngularJS. Many popular applications have been built using Electron, including the Slack desktop client application, the Atom and Microsoft Visual Code programming editors, the Postman REST client, the GitKraken GIT client, and Etcher, which makes it incredibly easy to burn OS images to flash drives to run on single-board computers.

Mobile applications: The Node.js for Mobile Systems project lets you develop smartphone or tablet computer applications using Node.js, for both iOS and Android. Apple's App Store rules preclude incorporating a JavaScript engine with JIT capabilities, meaning that normal Node.js cannot be used in an iOS application. For iOS application development, the project uses Node.js-on-ChakraCore to skirt around the App Store rules. For Android application development the project uses regular Node.js on Android. At the time of writing, the project is in an early stage of development, but it looks promising.

Internet of Things (IoT): Reportedly, it is a very popular language for Internet-of-Things projects, and Node.js does run on most ARM-based single-board computers. The clearest example is the NodeRED project. It offers a graphical programming environment, letting you draw programs by connecting blocks together. It features hardware-oriented input and output mechanisms, for example, to interact with General Purpose I/O (GPIO) pins on Raspberry Pi or Beaglebone single-board computers.

Server-side JavaScript

Quit scratching your head already! Of course you're doing it, scratching your head and mumbling to yourself, "What's a browser language doing on the server?" In truth, JavaScript has a long and largely unknown history outside the browser. JavaScript is a programming language, just like any other language, and the better question to ask is "Why should JavaScript remain trapped inside browsers?".

Back in the dawn of the web age, the tools for writing web applications were at a fledgling stage. Some were experimenting with Perl or TCL to write CGI scripts, and the PHP and Java languages had just been developed. Even then, JavaScript saw use on the server side. One early web application server was Netscape's LiveWire server, which used JavaScript. Some versions of Microsoft's ASP used JScript, their version of JavaScript. A more recent server-side JavaScript project is the RingoJS application framework in the Java universe. Java 6 and Java 7 were both shipped with the Rhino JavaScript engine. In Java 8, Rhino was dropped in favor of the newer Nashorn JavaScript engine.

In other words, JavaScript outside the browser is not a new thing, even if it is uncommon.

Why should you use Node.js?

Among the many available web application development platforms, why should you choose Node.js? There are many stacks to choose from; what is it about Node.js that makes it rise above the others? We will see in the following sections.

Popularity

Node.js is quickly becoming a popular development platform with adoption by plenty of big and small players. One of those is PayPal, who are replacing their incumbent Java-based system with one written in Node.js. For PayPal's blog post about this, visit https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/. Other large Node.js adopters include Walmart's online e-commerce platform, LinkedIn, and eBay.

According to NodeSource, Node.js usage is growing rapidly (visit https://nodesource.com/node-by-numbers). The measures include increasing bandwidth for downloading Node.js releases, increasing activity in Node.js-related GitHub projects, and more.

It's best to not just follow the crowd because the crowd claims their software platform does cool things. Node.js does some cool things, but more important is its technical merit.

JavaScript at all levels of the stack

Having the same programming language on the server and client has been a long-time dream on the web. This dream dates back to the early days of Java, where Java applets were to be the frontend to server applications written in Java, and JavaScript was originally envisioned as a lightweight scripting language for those applets. Java never fulfilled its hype as a client-side programming language, for various reasons. We ended up with JavaScript as the principle in-browser, client-side language, rather than Java. Typically, the frontend JavaScript developers were in a different language universe than the server-side team, who was likely to be coding in PHP, Java, Ruby, or Python.

Over time, in-browser JavaScript engines became incredibly powerful, letting us write ever-more complex browser-side applications. With Node.js, we may finally be able to implement applications with the same programming language on the client and server by having JavaScript at both ends of the web, in the browser and server.

A common language for frontend and backend offers several potential benefits:

The same programming staff can work on both ends of the wire

Code can be migrated between server and client more easily

Common data formats (JSON) exist between server and client

Common software tools exist for server and client

Common testing or quality reporting tools for server and client

When writing web applications, view templates can be used on both sides

The JavaScript language is very popular due to its ubiquity in web browsers. It compares favorably against other languages while having many modern, advanced language concepts. Thanks to its popularity, there is a deep talent pool of experienced JavaScript programmers out there.

Leveraging Google's investment in V8

To make Chrome a popular and excellent web browser, Google invested in making V8 a super-fast JavaScript engine. Google, therefore, has a huge motivation to keep on improving V8. V8 is the JavaScript engine for Chrome, and it can also be executed standalone. Node.js is built on top of the V8 JavaScript engine.

As Node.js becomes more important to the V8 team, there's a potential synergy of faster V8 performance wins as more people focus on V8 improvements.

Leaner, asynchronous, event-driven model

We'll get into this later. The Node.js architecture, a single execution thread, an ingenious event-oriented asynchronous-programming model, and a fast JavaScript engine, has less overhead than thread-based architectures.

Microservice architecture

A new sensation in software development is the microservice idea. Microservices are focused on splitting a large web application into small, tightly-focused services that can be easily developed by small teams. While they aren't exactly a new idea, they're more of a reframing of old client-server computing models, the microservice pattern fits well with agile project management techniques, and gives us more granular application deployment.  

Node.js is an excellent platform for implementing microservices. We'll get into this later.

Node.js is stronger for having survived a major schism and hostile fork

During 2014 and 2015, the Node.js community faced a major split over policy, direction, and control. The io.js project was a hostile fork driven by a group who wanted to incorporate several features and change who's in the decision-making process. The end result was a merge of the Node.js and io.js repositories, an independent Node.js foundation to run the show, and the community is working together to move forward in a common direction.

A concrete result of healing that rift is the rapid adoption of new ECMAScript language features. The V8 engine is adopting those new features quickly to advance the state of web development. The Node.js team, in turn, is adopting those features as quickly as they show up in V8, meaning that Promises and async functions are quickly becoming a reality for Node.js programmers.

The bottom line is that the Node.js community not only survived the io.js fork, but the community and the platform it nurtures grew stronger as a result.

Server utilization, the business bottom line, and green web hosting

The striving for optimal efficiency (handling more requests per second) is not just about the geeky satisfaction that comes from optimization. There are real business and environmental benefits. Handling more requests per second, as Node.js servers can do, means the difference between buying lots of servers and buying only a few servers. Node.js potentially lets your organization do more with less.

Roughly speaking, the more servers you buy, the greater the cost, and the greater the environmental impact of having those servers. There's a whole field of expertise around reducing costs and the environmental impact of running web server facilities, to which that rough guideline doesn't do justice. The goal is fairly obvious—fewer servers, lower costs, and a reduced environmental impact through utilizing more efficient software.

Intel's paper, Increasing Data Center Efficiency with Server Power Measurements (https://www.intel.com/content/dam/doc/white-paper/intel-it-data-center-efficiency-server-power-paper.pdf