O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Node.js Development

Book Description

A comprehensive, easy-to-follow guide to creating complete Node apps and understanding how to build, deploy, and test your own apps.

About This Book

  • Entirely project-based and practical
  • Explains the "Why" of Node.js features, not just the "how", providing you with a deep understanding and enabling you to easily apply concepts in your own applications
  • Covers the full range of technologies around Node.js – NPM, version control with Git, and much more

Who This Book Is For

This book targets anyone looking to launch their own Node applications, switch careers, or freelance as a Node developer. You should have a basic understanding of JavaScript in order to follow this course.

What You Will Learn

  • Learn the fundamentals of Node
  • Build apps that respond to user input
  • Master working with servers
  • Learn how to test and debug applications
  • Deploy and update your apps in the real world
  • Create responsive asynchronous web applications

In Detail

Learning Node.js Development is a practical, project-based book that provides you with all you need to get started as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developers' toolkit. If you are looking to create real-world Node applications, or you want to switch careers or launch a side project to generate some extra income, then you're in the right place. This book has been written around a single goal—turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications.

Learning Node.js Development is built from the ground up around the latest version of Node.js (version 9.x.x). You'll be learning all the cutting-edge features available only in the latest software versions.

This book cuts through the mass of information available around Node and delivers the essential skills that you need to become a Node developer. It takes you through creating complete apps and understanding how to build, deploy, and test your own Node apps. It maps out everything in a comprehensive, easy-to-follow package designed to get you up and running quickly.

Style and approach

This book is entirely project-based. From the very beginning you'll be programming every single app and completing various challenges designed to help test and reinforce what you've learned. There's no copying-and-pasting here. This book is about writing code and building projects.

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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. 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
  2. Getting Set Up
    1. Node.js installation
      1. Node.js version confirmation
      2. Installing Node
        1. Verifying installation
    2. What is Node?
      1. Differences between JavaScript coding using Node and in the browser
    3. Why use Node
      1. Blocking and non-blocking software development
        1. The working of blocking I/O
        2. The working non-blocking I/O
      2. Blocking and non-blocking examples using Terminal
      3. Node community – problem solving open source libraries
    4. Different text editors for node applications
    5. Hello World – creating and running the first Node app
      1. Creating the Node application
      2. Running the Node application
    6. Summary
  3. Node Fundamentals – Part 1
    1. Module basics
      1. Using case for require()
        1. Initialization of an application
        2. The built-in module to use require()
          1. Creating and appending files in the File System module
          2. The OS module in require()
          3. Concatenating user.username
          4. Using template strings
    2. Require own files
      1. Making a new file to load other files
      2. Exporting files from note.js to use in app.js
        1. A simple example of the working of the exports object
        2. Exporting the functions
          1. Exercise – adding a new function to the export object
          2. Solution to the exercise
    3. Third-party modules
      1. Creating projects using npm modules
      2. Installing the lodash module in our app
        1. Installation of lodash
        2. Using the utilities of lodash
          1. Using the _.isString utility
          2. Using _.uniq
      3. The node_modules folder
    4. Global modules
      1. Installing the nodemon module
      2. Executing nodemon
    5. Getting input
      1. Getting input from the user inside the command line
        1. Accessing the command-line argument for the notes application
          1. Adding if/else statements
          2. Exercise – adding two else if clauses to an if block
          3. Solution to the exercise
        2. Getting the specific note information
    6. Summary
  4. Node Fundamentals – Part 2
    1. yargs
      1. Installing yargs
      2. Running yargs
        1. Working with the add command
        2. Working with the list command
          1. The read command
          2. Dealing with the errors in parsing commands
          3. The remove command
        3. Fetching command
    2. JSON
      1. Converting objects into strings
      2. Defining a string and using in app as an object
      3. Converting a string back to an object
      4. Storing the string in a file
        1. Writing the file in the playground folder
        2. Reading out the content in the file
    3. Adding and saving notes
      1. Adding notes
        1. Adding notes to the notes array
        2. Fetching new notes
      2. Trying and catching code block
      3. Making the title unique
    4. Refactoring
      1. Moving functionality into individual functions
        1. Working with fetchNotes
        2. Working with saveNotes
      2. Testing the functionality
    5. Summary
  5. Node Fundamentals – Part 3
    1. Removing a note
      1. Using the removeNote function
      2. Printing a message of removing notes
    2. Reading note
      1. Using the getNote function
        1. Running the getNote function
      2. The DRY principle
        1. Using the logNote function
    3. Debugging
      1. Executing a program in debug mode
      2. Working with debugging
      3. Using debugger inside the notes application
    4. Listing notes
      1. Using the getAll function
    5. Advanced yargs
      1. Using chaining syntax on yargs
        1. Calling the .help command
        2. Adding the options object
          1. Adding the title
          2. Adding the body
      2. Adding support to the read and remove commands
        1. Adding the titleOption and bodyOption variables
        2. Testing the remove command
    6. Arrow functions
      1. Using the arrow function
      2. Exploring the difference between regular and arrow functions
        1. Exploring the arguments array
    7. Summary
  6. Basics of Asynchronous Programming in Node.js
    1. The basic concept of asynchronous program
      1. Illustrating the async programming model
    2. Call stack and event loop
      1. A synchronous program example
        1. The call stack
        2. Running the synchronous program
        3. A complex synchronous program example
      2. An async program example
        1. The Node API in async programming
        2. The callback queue in async programming
        3. The event loop
        4. Running the async code
    3. Callback functions and APIs
      1. The callback function
        1. Creating the callback function
          1. Running the callback function
        2. Simulating delay using setTimeout
      2. Making request to Geolocation API
        1. Using Google Maps API data in our code
          1. Installing the request package
          2. Using request as a function
          3. Running the request
      3. Pretty printing objects
        1. Using the body argument
    4. Making up of the HTTPS requests
      1. The response object
      2. The error argument
      3. Printing data from the body object
        1. Printing the formatted address
        2. Printing latitude and longitude
    5. Summary
  7. Callbacks in Asynchronous Programming
    1. Encoding user input
      1. Installing yargs
      2. Configuring yargs
        1. Printing the address to screen
      3. Encoding and decoding the strings
        1. Encoding URI component
        2. Decoding URI component
      4. Pulling the address out of argv
    2. Callback errors
      1. Checking error in Google API request
        1. Adding the if statement for callback errors
        2. Adding if else statement to check body status property
          1. Testing the body status property
    3. Abstracting callbacks
      1. Refactoring app.js and code into geocode.js file
        1. Working on request statement
        2. Creating geocode file
      2. Adding callback function to geocodeAddress
        1. Setting up the function in geocodeAddress function in app.js
        2. Implementing the callback function in geocode.js file
        3. Testing the callback function in geocode.js file
    4. Wiring up weather search
      1. Exploring working of API in the browser
        1. Exploring the actual URL for code
      2. Making a request for the weather app using the static URL
      3. Error handling in the the callback function
        1. Another way of error handling
        2. Testing the error handling in callback
    5. Chaining callbacks together
      1. Refactoring our request call in weather.js file
        1. Defining the new function getWeather in weather file
        2. Providing weather directory in app.js
        3. Passing the arguments in the getWeather function
          1. Printing errorMessage in the getWeather function
        4. Implementing getWeather callback inside weather.js file
          1. Adding dynamic latitude and longitude
          2. Changing console.log calls into callback calls
      2. Chaining the geocodeAddress and getWeather callbacks together
        1. Moving getWeather call into geocodeAddress function
        2. Replacing static coordinates with dynamic coordinates
        3. Testing the chaining of callbacks
    6. Summary
  8. Promises in Asynchronous Programming
    1. Introduction to ES6 promises
      1. Creating an example promise
        1. Calling the promise method then
      2. Running the promise example in Terminal
      3. Error handling in promises
      4. Merits of promises
    2. Advanced promises
      1. Providing input to promises
        1. Returning the promises
      2. Promise chaining
        1. Error handling in promises chaining
          1. The catch method
      3. The request library in promises
        1. Testing the request library
    3. Weather app with promises
      1. Fetching weather app code from the app.js file
      2. Axios documentations
      3. Installing axios
      4. Making calls in the app-promise file
        1. Making axios request
        2. Error handling in axios request
          1. Error handling with ZERO_RESULT body status
      5. Generating the weather URL
      6. Chaining the promise calls
    4. Summary
  9. Web Servers in Node
    1. Introducing Express
      1. Configuring Express
        1. Express docs website
        2. Installing Express
      2. Creating an app
        1. Exploring the developer tools in the browser for the app request
        2. Passing HTML to res.send
      3. Sending JSON data back
        1. Error handling in the JSON request
    2. The static server
      1. Making an HTML page
        1. The head tag
        2. The body tag
      2. Serving the HTML page in the Express app
        1. The call to app.listen
    3. Rendering templates
      1. Installing the hbs module
      2. Configuring handlebars
      3. Our first template
        1. Getting the static page for rendering
        2. Injecting data inside of templates
        3. Rendering the template for the root of the website
    4. Advanced templates
      1. Adding partials
        1. Working of partial
        2. The Header partial
      2. The Handlebars helper
        1. Arguments in Helper
    5. Express Middleware
      1. Exploring middleware
        1. Creating a logger
        2. Printing message to file
      2. The maintenance middleware without the next object
        1. Testing the maintenance middleware
    6. Summary
  10. Deploying Applications to Web
    1. Adding version control
      1. Installing Git
        1. Git on macOS
        2. Git on Windows
        3. Testing the installation
      2. Turning the node-web-server directory into a Git repository
      3. Using Git
        1. Adding untracked files to commit
        2. Making a commit
    2. Setting up GitHub and SSH keys
      1. Setting up SSH keys
        1. SSH keys documentations
        2. Working on commands
          1. Generating a key
        3. Starting up the SSH agent
      2. Configuring GitHub
        1. Testing the configuration
      3. Creating a new repository
        1. Setting up the repository
    3. Deploying the node app to the Web
      1. Installing Heroku command-line tools
        1. Log in to Heroku account locally
        2. Getting SSH key to Heroku
      2. Setting up in the application code for Heroku
        1. Changes in the server.js file
        2. Changes in the package.json file
      3. Making a commit in Heroku
      4. Running the Heroku create command
    4. Summary
  11. Testing the Node Applications – Part 1
    1. Basic testing
      1. Installing the testing module
      2. Testing a Node project
        1. Mocha – the testing framework
        2. Creating a test file for the add function
          1. Creating the if condition for the test
        3. Testing the squaring a number function
      3. Autorestarting the tests
    2. Using assertion libraries in testing Node modules
      1. Exploring assertion libraries
      2. Chaining multiple assertions
        1. Multiple assertions for the square function
      3. Exploring usage of expect with bogus test
        1. Using toBe and toNotBe to compare array/objects
        2. Using the toEqual and toNotEqual assertions
        3. Using toInclude and toExclude
      4. Testing the setName method
    3. The asynchronous testing
      1. Creating the asyncAdd function using the setTimeout object
      2. Writing the test for the asyncAdd function
        1. Making assertion for the asyncAdd function
        2. Adding the done argument
      3. The asynchronous testing for the square function
        1. Creating the async square function
        2. Writing test for asyncSquare
          1. Making assertions for the asyncSquare function
    4. Summary
  12. Testing the Node Applications – Part 2
    1. Testing the Express application
      1. Setting up testing for the Express app
      2. Testing the Express app using SuperTest
        1. The SuperTest documentation
        2. Creating a test for the Express app
        3. Writing the test for the Express app
          1. Testing our first API request
          2. Setting up custom status
          3. Adding flexibility to SuperTest
      3. Creating an express route
        1. Writing the test for the express route
    2. Organizing test with describe()
      1. Adding describe() for individual methods
      2. Adding the route describe block for the server.test.js file
    3. Test spies
      1. Creating a test file for spies
      2. Creating a spy
      3. Setting up spies assertions
        1. More details out of spy assertion
      4. Swapping of the function with spy
        1. Installing and setting up the rewire function
        2. Replacing db with the spy
        3. Writing a test to verify swapping of the function
    4. Summary
    5. Conclusion
  13. Another Book You May Enjoy
    1. Leave a review - let other readers know what you think