Mastering Node.js - Second Edition

Book description

Expert techniques for building fast servers and scalable, real-time network applications with minimal effort; rewritten for Node.js 8 and Node.js 9

About This Book

  • Packed with practical examples and explanations, Mastering Node.js contains everything you need to take your applications to the next level.
  • Unleash the full potential of Node.js 9 to build real-time and scalable applications.
  • Gain in-depth knowledge of asynchronous programming, event loops, and parallel data processing.
  • Explore Node's innovative event-non-blocking design, and build professional applications with the help of detailed examples.

Who This Book Is For

This book is targeted at JavaScript developers who want to take an in-depth look at the latest Node.js framework to create faster, scalable, real-time backend applications. Basic JavaScript programming knowledge—and also some previous Node.js development experience—are mandatory to get the best out of this book

What You Will Learn

  • Build an Electron desktop app using Node that manages a filesystem
  • Explore Streams and understand how they apply to building networked services
  • Develop and deploy an SMS-driven customer service application
  • Use WebSockets for rapid bi-directional communication
  • Construct serverless applications with Amazon Lambda
  • Test and debug with headless browsers, CPU profiling, Mocha, Sinon, and more
  • Scale applications vertically and horizontally across multiple cores and web services

In Detail

Node.js, a modern development environment that enables developers to write server- and client-side code with JavaScript, thus becoming a popular choice among developers.

This book covers the features of Node that are especially helpful to developers creating highly concurrent real-time applications. It takes you on a tour of Node's innovative event non-blocking design, showing you how to build professional applications. This edition has been updated to cover the latest features of Node 9 and ES6. All code examples and demo applications have been completely rewritten using the latest techniques, introducing Promises, functional programming, async/await, and other cutting-edge patterns for writing JavaScript code. Learn how to use microservices to simplify the design and composition of distributed systems. From building serverless cloud functions to native C++ plugins, from chatbots to massively scalable SMS-driven applications, you'll be prepared for building the next generation of distributed software.

By the end of this book, you'll be building better Node applications more quickly, with less code and more power, and know how to run them at scale in production environments.

Style and approach

Mastering Node.js contains all of the examples and explanations you'll need to build applications in a short amount of time and at a low cost, running rapidly and at scale.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Understanding the Node Environment
    1. Introduction – JavaScript as a systems language
      1. The Unix design philosophy
      2. POSIX
      3. Events for everything
    2. Standard libraries
    3. Extending JavaScript
      1. Events
      2. Modularity
      3. The network
    4. V8, JavaScript, and optimizations
      1. Flags
      2. Optimizing your code
        1. Numbers and tracing optimization/de-optimization
        2. Objects and arrays
        3. Functions
      3. Optimized JavaScript
        1. Help with variables
        2. Arrow functions
        3. String manipulation
    5. The process object
    6. The REPL
    7.  Summary
  3. Understanding Asynchronous Event-Driven Programming
    1. Node's unique design
      1. Collaboration
      2. Queueing
    2. Understanding the event loop
      1. Event loop ordering, phases, and priorities
    3. Listening for events
      1. Signals
      2. Child processes
      3. File events
      4. Deferred execution
        1. process.nextTick
          1. setImmediate
    4. Timers
      1. setTimeout
      2.  setInterval
      3. unref and ref
    5. Concurrency and errors
      1. Managing concurrency
        1. Callbacks
        2. Promises
        3. async/await
        4. Generators and Iterators
      2. Errors and exceptions
      3. Considerations
    6. Building a Twitter feed using file events
    7. Summary
  4. Streaming Data Across Nodes and Clients
    1. Why use streams?
    2. Exploring streams
      1. Implementing readable streams
        1. Pushing and pulling
      2. Writable streams
      3. Duplex streams
      4. Transforming streams
      5. Using PassThrough streams
    3. Creating an HTTP server
      1. Making HTTP requests
      2. Proxying and tunneling
    4. HTTPS, TLS (SSL), and securing your server
      1. Creating a self-signed certificate for development
      2. Installing a real SSL certificate
    5. The request object
      1. The URL module
      2. The Querystring module
    6. Working with headers
      1. Using cookies
      2. Understanding content types
      3. Handling favicon requests
    7. Handling POST data
    8. Creating and streaming images with Node
      1. Creating, caching, and sending a PNG representation
    9. Summary
  5. Using Node to Access the Filesystem
    1. Directories, and iterating over files and folders
      1. Types of files
      2. File paths
      3. File attributes
      4. Opening and closing files
        1., flags, [mode], callback)
        2. fs.close(fd, callback)
      5. File operations
        1. fs.rename(oldName, newName, callback)
        2. fs.truncate(path, len, callback)
        3. fs.ftruncate(fd, len, callback)
        4. fs.chown(path, uid, gid, callback)
        5. fs.fchown(fd, uid, gid, callback)
        6. fs.lchown(path, uid, gid, callback)
        7. fs.chmod(path, mode, callback)
        8. fs.fchmod(fd, mode, callback) ----
        9. fs.lchmod(path, mode, callback)
        10., dstPath, callback)
        11. fs.symlink(srcPath, dstPath, [type], callback)
        12. fs.readlink(path, callback)
        13. fs.realpath(path, [cache], callback)
        14. fs.unlink(path, callback)
        15. fs.rmdir(path, callback)
        16. fs.mkdir(path, [mode], callback)
        17. fs.exists(path, callback)
        18. fs.fsync(fd, callback)
      6. Synchronicity
      7. Moving through directories
    2. Reading from a file
      1. Reading byte by byte
        1., buffer, offset, length, position, callback)
      2. Fetching an entire file at once
        1. fs.readFile(path, [options], callback)
      3. Creating a readable stream
        1. fs.createReadStream(path, [options])
      4. Reading a file line by line
        1. The Readline module
    3. Writing to a file
      1. Writing byte by byte
        1. fs.write(fd, buffer, offset, length, position, callback)
      2. Writing large chunks of data
        1. fs.writeFile(path, data, [options], callback)
        2. fs.appendFile(path, data, [options], callback)
      3. Creating a writable stream
        1. fs.createWriteStream(path, [options])
      4. Caveats
    4. Serving static files
      1. Redirecting requests
        1. Location
        2. Content-Location
      2. Implementing resource caching
    5. Handling file uploads
      1. Putting it all together
    6. A simple file browser
      1. Electron
        1. Electron processes
        2. The renderer process
      2. Vue.js
    7. Summary
  6. Managing Many Simultaneous Client Connections
    1. Understanding concurrency
      1. Concurrency is not parallelism
    2. Routing requests
      1. Understanding routes
      2. Using Express to route requests
    3. Using Redis for tracking client state
      1. Storing user data
    4. Handling sessions
      1. Cookies and client state
      2. A simple poll
    5. Authenticating connections
      1. Basic authentication
      2. Handshaking
      3. Using JSON Web Tokens for authentication
    6. Summary
    7. Further reading
  7. Creating Real-Time Applications
    1. Introducing AJAX
      1. Responding to calls
      2. Creating a stock ticker
    2. Bidirectional communication with
      1. Using the WebSocket API
      3. Drawing collaboratively
    3. Listening for Server Sent Events
      1. Using the EventSource API
      2. The EventSource stream protocol
      3. Asking questions and getting answers
    4. Building a collaborative document editing application
    5. Summary
  8. Using Multiple Processes
    1. Node's single-threaded model
      1. The benefits of single-threaded programming
      2. Multithreading is already native and transparent
    2. Creating child processes
      1. Spawning processes
      2. Forking processes
      3. Buffering process output
    3. Communicating with your child
      1. Sending messages to children
    4. Parsing a file using multiple processes
      1. Using the cluster module
      2. Cluster events
      3. Worker object properties
      4. Worker events
    5. Using PM2 to manage multiple processes
      1. Monitoring
      2. Process files
    6. Real-time activity updates of multiple worker results
    7.  Summary
  9. Scaling Your Application
    1. When to scale?
      1. Network latency
      2. Hot CPUs
      3. Socket usage
      4. Many file descriptors
      5. Data creep
      6. Tools for monitoring servers
    2. Running multiple Node servers
      1. Forward and reverse proxies
      2. Using the http-proxy module
        1. Deploying a NGINX load balancer on Digital Ocean
        2. Installing and configuring NGINX
    3. Message queues – RabbitMQ
      1. Types of exchanges
    4. Using Node's UDP module
      1. UDP multicasting with Node
    5. Using Amazon Web Services in your application
      1. Authenticating
      2. Errors
      3. Using S3 to store files
        1. Working with buckets
        2. Working with objects
        3. Using AWS with a Node server
      4. Getting and setting data with DynamoDB
        1. Searching the database
      5. Sending mail via SES
    6. Using Twilio to create an SMS bot on Heroku
      1. Using Twilio webhooks to receive and send SMS messages
        1. The switchboard
        2. The ThankYou interface
    7. Summary
  10. Microservices
    1. Why microservices?
    2. From 3-Tiers to 4-Tiers
      1. Monoliths
      2. From monoliths to 3-Tiered architectures
        1. How did this architecture come about?
      3. Service-Oriented Architectures
      4. 4-Tiers and microservices
    3. Deploying microservices
      1. Microservices with Seneca
    4. Serverless applications
      1. AWS Lambda
      2.  Scaling with Claudia and API Gateway
        1. Installing claudia and deploying a service
    5. Containerized microservices
      1. Getting started with Docker
        1. Creating a Dockerfile
        2. Running containers
      2. Orchestrating Containers with Kubernetes
        1. Creating a basic Kubernetes cluster
        2. Declaring Pod deployments
    6. Summary
  11. Testing Your Application
    1. Why testing is important
      1. Unit tests
      2. Functional tests
      3. Integration tests
    2. Native Node testing and debugging tools
      1. Writing to the console
        1. Formatting console output
          1. The util.format(format, [arg, arg…]) method
          2. The util.inspect(object, [options]) method
      2. The Node debugger
      3. The assert module
      4. Sandboxing
        1. Distinguishing between local scope and execution context
        2. Using compiled contexts
    3. Testing with Mocha, Chai, and Sinon
      1. Mocha
      2. Chai
      3. Sinon
        1. Spies
        2. Stubs
        3. Mocks
    4. Headless testing with Nightmare and Puppeteer
      1. Nightmare
      2. Puppeteer
    5. Testing the terrain
    6. Testing processes, memory, and CPU
      1. Profiling processes
      2. Dumping the heap
      3. Connecting Node to Chrome DevTools
        1. CPU profiling
        2. Live debugging
    7. Summary
  12. Organizing Your Work into Modules
    1. How to load and use modules
    2. The module object
      1. Modules, exports, and module.exports
      2. Modules and caching
    3. How Node handles module paths
    4. Creating a package file
      1. Easy init
      2. Adding scripts to package.json
        1. npm as a build system using custom scripts
      3. Registering package dependencies
      4. Publishing and managing NPM packages
      5. Global installs and binaries
      6. Other repositories
      7. Lockfiles
  13. Creating Your Own C++ Add-ons
    1. Hello World
    2. A calculator
    3. Using NAN
      1. Hello, nan
      2. Asynchronous add-ons
    4. Closing thoughts
    5. Links and resources

Product information

  • Title: Mastering Node.js - Second Edition
  • Author(s): Sandro Pasquali, Kevin Faaborg
  • Release date: December 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781785888960