Node.js Complete Reference Guide

Book Description

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.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Node.js Complete Reference Guide
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the authors
    2. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. About Node.js
    1. The capabilities of Node.js
      1. Server-side JavaScript
    2. Why should you use Node.js?
      1. Popularity
      2. JavaScript at all levels of the stack
      3. Leveraging Google's investment in V8
      4. Leaner, asynchronous, event-driven model
      5. Microservice architecture
      6. Node.js is stronger for having survived a major schism and hostile fork
    3. Threaded versus event-driven architecture
      1. Performance and utilization
        1. Is Node.js a cancerous scalability disaster?
      2. Server utilization, the business bottom line, and green web hosting
    4. Embracing advances in the JavaScript language
      1. Deploying ES2015/2016/2017/2018 JavaScript code
    5. Node.js, the microservice architecture, and easily testable systems
    6. Node.js and the Twelve-Factor app model
    7. Summary
  7. Setting up Node.js
    1. System requirements
    2. Installing Node.js using package managers
      1. Installing on macOS with MacPorts
      2. Installing on macOS with Homebrew
      3. Installing on Linux, *BSD, or Windows from package management systems
        1. Installing Node.js in the Windows Subsystem for Linux (WSL)
        2. Opening an administrator-privileged PowerShell on Windows
      4. Installing the Node.js distribution from
    3. Installing from source on POSIX-like systems
      1. Installing prerequisites
      2. Installing developer tools on macOS
      3. Installing from source for all POSIX-like systems
      4. Installing from source on Windows
    4. Installing multiple Node.js instances with nvm
      1. Installing nvm on Windows
    5. Native code modules and node-gyp
    6. Node.js versions policy and what to use
    7. Editors and debuggers
    8. Running and testing commands
      1. Node.js's command-line tools
      2. Running a simple script with Node.js
      3. Conversion to async functions and the Promise paradigm
      4. Launching a server with Node.js
    9. NPM – the Node.js package manager
    10. Node.js, ECMAScript 2015/2016/2017, and beyond 
      1. Using Babel to use experimental JavaScript features
    11. Summary
  8. Node.js Modules
    1. Defining a module
      1. CommonJS and ES2015 module formats
        1. CommonJS/Node.js module format
        2. ES6 module format
        3. JSON modules
        4. Supporting ES6 modules on older Node.js versions
      2. Demonstrating module-level encapsulation
    2. Finding and loading CommonJS and JSON modules using require
      1. File modules
      2. Modules baked into Node.js binary
      3. Directories as modules
      4. Module identifiers and pathnames
      5. An example of application directory structure
    3. Finding and loading ES6 modules using import
    4. Hybrid CommonJS/Node.js/ES6 module scenarios
      1. Dynamic imports with import()
      2. The import.meta feature
    5. npm - the Node.js package management system
      1. The npm package format
      2. Finding npm packages
      3. Other npm commands
      4. Installing an npm package
        1. Installing a package by version number
        2. Global package installs
        3. Avoiding global module installation
      5. Maintaining package dependencies with npm
        1. Automatically updating package.json dependencies
        2. Fixing bugs by updating package dependencies
      6. Packages that install commands
        1. Configuring the PATH variable to handle commands installed by modules
        2. Configuring the PATH variable on Windows
        3. Avoiding modifications to the PATH variable
      7. Updating outdated packages you've installed
      8. Installing packages from outside the npm repository
      9. Initializing a new npm package
      10. Declaring Node.js version compatibility
      11. Publishing an npm package
      12. Explicitly specifying package dependency version numbers
    6. The Yarn package management system
    7. Summary
  9. HTTP Servers and Clients
    1. Sending and receiving events with EventEmitters
      1. JavaScript classes and class inheritance
      2. The EventEmitter Class
      3. The EventEmitter theory
    2. HTTP server applications
    3. ES2015 multiline and template strings
    4. HTTP Sniffer – listening to the HTTP conversation
    5. Web application frameworks
    6. Getting started with Express
      1. Setting environment variables in Windows cmd.exe command line
      2. Walking through the default Express application
      3. The Express middleware
      4. Middleware and request paths
      5. Error handling
    7. Calculating the Fibonacci sequence with an Express application
      1. Computationally intensive code and the Node.js event loop
        1. Algorithmic refactoring
    8. Making HTTP Client requests
    9. Calling a REST backend service from an Express application
      1. Implementing a simple REST server with Express
      2. Refactoring the Fibonacci application for REST
      3. Some RESTful modules and frameworks
    10. Summary
  10. Your First Express Application
    1. Promises, async functions, and Express router functions
      1. Promises and error handling
      2. Flattening our asynchronous code
      3. Promises and generators birthed async functions
    2. Express and the MVC paradigm
    3. Creating the Notes application
      1. Your first Notes model
        1. Understanding ES-2015 class definitions
        2. Filling out the in-memory Notes model
      2. The Notes home page
      3. Adding a new note – create
      4. Viewing notes – read
      5. Editing an existing note – update
      6. Deleting notes – destroy
    4. Theming your Express application
    5. Scaling up – running multiple Notes instances
    6. Summary
  11. Implementing the Mobile-First Paradigm
    1. Problem – the Notes app isn't mobile friendly
    2. Mobile-first paradigm
    3. Using Twitter Bootstrap on the Notes application
      1. Setting it up
      2. Adding Bootstrap to application templates
      3. Alternative layout frameworks
    4. Flexbox and CSS Grids
    5. Mobile-first design for the Notes application
      1. Laying the Bootstrap grid foundation
      2. Responsive page structure for the Notes application
      3. Using icon libraries and improving visual appeal
      4. Responsive page header navigation bar
      5. Improving the Notes list on the front page
      6. Cleaning up the Note viewing experience
      7. Cleaning up the add/edit note form
      8. Cleaning up the delete-note window
    6. Building a customized Bootstrap
      1. Pre-built custom Bootstrap themes
    7. Summary
  12. Data Storage and Retrieval
    1. Data storage and asynchronous code
    2. Logging
      1. Request logging with Morgan
      2. Debugging messages
      3. Capturing stdout and stderr
      4. Uncaught exceptions
      5. Unhandled Promise rejections
    3. Using the ES6 module format
      1. Rewriting app.js as an ES6 module
      2. Rewriting bin/www as an ES6 module
      3. Rewriting models code as ES6 modules
      4. Rewriting router modules as ES6 modules
    4. Storing notes in the filesystem
      1. Dynamic import of ES6 modules
      2. Running the Notes application with filesystem storage
    5. Storing notes with the LevelUP data store
    6. Storing notes in SQL with SQLite3
      1. SQLite3 database schema
      2. SQLite3 model code
      3. Running Notes with SQLite3
    7. Storing notes the ORM way with Sequelize
      1. Sequelize model for the Notes application
      2. Configuring a Sequelize database connection
      3. Running the Notes application with Sequelize
    8. Storing notes in MongoDB
      1. MongoDB model for the Notes application
      2. Running the Notes application with MongoDB
    9. Summary
  13. Multiuser Authentication the Microservice Way
    1. Creating a user information microservice
      1. User information model
      2. A REST server for user information
      3. Scripts to test and administer the user authentication server
      4. Login support for the Notes application
        1. Accessing the user authentication REST API
        2. Login and logout routing functions
        3. Login/logout changes to app.js
        4. Login/logout changes in routes/index.mjs
        5. Login/logout changes required in routes/notes.mjs
        6. View template changes supporting login/logout
        7. Running the Notes application with user authentication
      5. Twitter login support for the Notes application
        1. Registering an application with Twitter
        2. Implementing TwitterStrategy
    2. Securely keeping secrets and passwords
    3. The Notes application stack
    4. Summary
  14. Dynamic Client/Server Interaction with Socket.IO
    1. Introducing Socket.IO
    2. Initializing Socket.IO with Express
    3. Real-time updates on the Notes homepage
      1. The Notes model as an EventEmitter class
      2. Real-time changes in the Notes home page
        1. Changing the homepage and layout templates
        2. Running Notes with real-time homepage updates
      3. Real-time action while viewing notes
        1. Changing the note view template for real-time action
        2. Running Notes with real-time updates while viewing a note
    4. Inter-user chat and commenting for Notes
      1. Data model for storing messages
      2. Adding messages to the Notes router
      3. Changing the note view template for messages
        1. Using a Modal window to compose messages
        2. Sending, displaying, and deleting messages
        3. Running Notes and passing messages
        4. Other applications of Modal windows
    5. Summary
  15. Deploying Node.js Applications
    1. Notes application architecture and deployment considerations
    2. Traditional Linux Node.js service deployment
      1. Prerequisite – provisioning the databases
      2. Installing Node.js on Ubuntu
      3. Setting up Notes and user authentication on the server
        1. Adjusting Twitter authentication to work on the server
      4. Setting up PM2 to manage Node.js processes
    3. Node.js microservice deployment with Docker
      1. Installing Docker on your laptop
        1. Starting Docker with Docker for Windows/macOS
        2. Kicking the tires of Docker
      2. Creating the AuthNet for the user authentication service
        1. MySQL container for Docker
        2. Initializing AuthNet
        3. Script execution on Windows
        4. Linking Docker containers
        5. The db-userauth container
        6. Dockerfile for the authentication service
        7. Configuring the authentication service for Docker
        8. Building and running the authentication service Docker container
        9. Exploring Authnet
      3. Creating FrontNet for the Notes application
        1. MySQL container for the Notes application
        2. Dockerizing the Notes application
        3. Controlling the location of MySQL data volumes
        4. Docker deployment of background services
      4. Deploying to the cloud with Docker compose
        1. Docker compose files
        2. Running the Notes application with Docker compose
      5. Deploying to cloud hosting with Docker compose
    4. Summary
  16. Unit Testing and Functional Testing
    1. Assert – the basis of testing methodologies
    2. Testing a Notes model
      1. Mocha and Chai­ – the chosen test tools
      2. Notes model test suite
        1. Configuring and running tests
        2. More tests for the Notes model
        3. Testing database models
    3. Using Docker to manage test infrastructure
      1. Docker Compose to orchestrate test infrastructure
      2. Executing tests under Docker Compose
        1. MongoDB setup under Docker and testing Notes against MongoDB
    4. Testing REST backend services
    5. Automating test results reporting
    6. Frontend headless browser testing with Puppeteer
      1. Setting up Puppeteer
      2. Improving testability in the Notes UI
      3. Puppeteer test script for Notes
        1. Running the login scenario
        2. The Add Note scenario
      4. Mitigating/preventing spurious test errors in Puppeteer scripts
        1. Configuring timeouts
        2. Tracing events on the Page and the Puppeteer instance
        3. Inserting pauses
        4. Avoiding WebSockets conflicts
        5. Taking screenshots
    7. Summary
  17. REST – What You Did Not Know
    1. REST fundamentals
      1. Principle 1 – Everything is a resource
      2. Principle 2 – Each resource is identifiable by a unique identifier
      3. Principle 3 – Manipulate resources via standard HTTP methods
      4. Principle 4 – Resources can have multiple representations
      5. Principle 5 – Communicate with resources in a stateless manner
    2. The REST goals
      1. Separation of the representation and the resource
      2. Visibility
      3. Reliability
      4. Scalability and performance
    3. Working with WADL
    4. Documenting RESTful APIs with Swagger
      1. Taking advantage of the existing infrastructure
    5. Summary
  18. Building a Typical Web API
    1. Specifying the API
    2. Implementing routes
    3. Querying the API using test data
    4. Content negotiation
    5. API versioning
    6. Self-test questions
    7. Summary
  19. Using NoSQL Databases
    1. MongoDB – a document store database
    2. Database modeling with Mongoose
    3. Testing a Mongoose model with Mocha
    4. Creating a user-defined model around a Mongoose model
    5. Wiring up a NoSQL database module to Express
    6. Self-test questions
    7. Summary
  20. Restful API Design Guidelines
    1. Endpoint URLs and HTTP status codes best practices
    2. Extensibility and versioning
    3. Linked data
    4. Summary
  21. Implementing a Full Fledged RESTful Service
    1. Working with arbitrary data
    2. Linking
    3. Implementing paging and filtering
    4. Caching
      1. Supplying the Cache-Control header in Express applications
    5. Discovering and exploring RESTful services
    6. Summary
  22. Consuming a RESTful API
    1. Consuming RESTful services with jQuery
    2. Troubleshooting and identifying problems on the wire
    3. Cross Origin Resource Sharing
    4. Content Delivery Networks
    5. Handling HTTP status codes on the client side
    6. Summary
  23. Securing the Application
    1. Authentication
      1. Basic authentication
      2. Passport
        1. Passport's basic authentication strategy
        2. Passport's OAuth Strategy
        3. Passport's third-party authentication strategies
    2. Authorization
    3. Transport layer security
      1. Self-test questions
    4. Summary
  24. The Age of Microservices
    1. From monolith to microservices
    2. Patterns of microservices
      1. Decomposable
      2. Autonomous
      3. Scalable
      4. Communicable
    3. Summary
  25. Modules and Toolkits
    1. Seneca
    2. Hydra
    3. Summary
  26. Building a Microservice
    1. Using Hydra
    2. Using Seneca
      1. Plugins
    3. Summary
  27. State
    1. State
      1. Storing state
      2. MySQL
      3. RethinkDB
      4. Redis
      5. Conclusion
    2. Security
    3. Summary
  28. Testing
    1. Integrating tests
      1. Using chai
      2. Adding code coverage
      3. Covering all code
    2. Mocking our services
    3. Summary
  29. Design Patterns
    1. Choosing patterns
      1. Architectural patterns
        1. Front Controller
        2. Layered
        3. Service Locator
        4. Observer
        5. Publish-Subscribe
    2. Using patterns
      1. Planning your microservice
      2. Obstacles when developing
    3. Summary
  30. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Node.js Complete Reference Guide
  • Author(s): Valentin Bojinov, David Herron, Diogo Resende
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789952117