Advanced Node.js Development

Book description

Takes you through creating your own API, building a full real-time web app, securing your Node systems, and practical applications of the latest Async and Await technologies. It maps out everything in a comprehensive, easy-to-follow package designed to get you up and running quickly.

About This Book

  • Entirely project-based and practical
  • Explains the "why" of Node.js features, not just the "how", providing 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, MongoDB, version control with Git, and many more

Who This Book Is For

This book is for 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 book. This book follows directly on from Learning Node.js Development, but more advanced readers can benefit from this book without having read the first part.

What You Will Learn

  • Develop, test, and deploy real-world Node.js applications
  • Master Node.js by building practical, working examples
  • Use awesome third-party Node modules such as MongoDB, Mongoose, Socket.io, and Express
  • Create real-time web applications
  • Explore async and await in ES7

In Detail

Advanced Node.js Development is a practical, project-based book that provides you with all you need to progress as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developer's toolkit. If you're 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 was written around a single goal: turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications.

There's no better time to dive in. According to the 2018 Stack Overflow Survey, Node is in the top ten for back-end popularity and back-end salary. This book 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 delivers advanced skills that you need to become a professional Node developer. Along this journey you'll create your own API, you'll build a full real-time web app and create projects that apply the latest Async and Await technologies. Andrew Mead maps everything out for you in this book so that you can learn how to build powerful Node.js projects 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.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Advanced Node.js Development
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    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. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Set Up
    1. Installing MongoDB and Robomongo for Linux and macOS
    2. Installing MongoDB and Robomongo for Windows
      1. Creating and reading data
    3. Summary
  7. MongoDB, Mongoose, and REST APIs – Part 1
    1. Connecting to MongoDB and writing data
      1. Creating a directory for the project
      2. Connecting the mongodb-connect file to the database
        1. Adding a string as the first argument
        2. Adding the callback function as the second argument
        3. Error handling in mongodb-connect
        4. Running the file in the Terminal
      3. Adding data to the database
      4. Adding a new record into a collection
    2. The ObjectId
      1. The _id property in the context of MongoDB
        1. Calling the .getTimestamp function
      2. Using object destructuring ES6
        1. Creating a new instance of objectID
    3. Fetching data
      1. Fetching todos in Robomongo file
      2. The find method
        1. Writing a query to fetch certain values
        2. Writing a query to fetch completed todos
        3. Qureying todos by id
      3. Implementing the count method
      4. Querying users collection
    4. Setting up the repo
    5. Deleting documents
      1. Exploring methods to delete data
        1. The deleteMany method
        2. The deleteOne Method
        3. The deleteOne method
        4. The findOneAndDelete method
      2. Using the deleteMany and findOneAndDelete methods
        1. Removing duplicate documents
        2. Targeting the documents using ID
        3. Running the findOneAndDelete and deleteMany statements
      3. Making commit for the deleting documents methods
    6. Updating data
    7. Summary
  8. MongoDB, Mongoose, and REST APIs – Part 2
    1. Setting up Mongoose
      1. Setting up root of the project
      2. Connecting mongoose to database
      3. Creating the todo model
        1. Creating a brand-new Todo
        2. Saving the instance to the database
      4. Running the Todos script
      5. Creating a second Todo model
    2. Validators, Types, and Defaults
      1. Mongoose validators
        1. Customizing the Todo text property
      2. Mongoose defaults
      3. Mongoose types
      4. Creating a Mongoose user model for authentication
        1. Setting up the email property
    3. Installing Postman
      1. Making an HTTP request to Google
      2. Illustrating working of the JSON data
    4. Resource Creation Endpoint - POST /todos
      1. Refactoring the server.js file to create POST todos route
        1. Configuring the Todo and Users file
          1. Loading Todo and User file in server.js
      2. Configuring the Express application
      3. Configuring the POST route
        1. Getting body data from the client
        2. Creating an instance of Mongoose model
          1. Setting up HTTP status code
        3. Testing POST /todos inside of Postman
        4. Adding more Todos to the database
    5. Testing POST /todos
      1. Installing npm modules for testing POST /todos route
      2. Setting up the test files
        1. Loading the test files
        2. Adding describe block for the test cases
          1. Making the POST requests via supertest 
          2. Making assertions about the POST request
          3. Making a request to fetch the Todos from the database
          4. Adding the catch call for the error handling
        3. Setting up test scripts in package.json
        4. Adding testing life cycle method in server.test.js file
        5. Running the test suite
      3. Test case: should not create todo with invalid body data
        1. Making assertions about the length of the Todos collection
      4. Making commit for POST /todos route
    6. List Resources - GET /todos
      1. Creating the GET /todos route
      2. Testing the GET /todos route 
      3. Setting up Post request to create a todo
    7. Testing GET /todos
      1. Adding seed data for the GET /todos test case
        1. Adding a describe block to the test case
        2. Adding assertions to the test case
    8. Summary
  9. MongoDB, Mongoose, and REST APIs – Part 3
    1. Mongoose queries and ID validation
      1. Todo.find method
      2. Todo.findOne method
      3. Todo.findById method
      4. Handling situations where the ID doesn't exist
      5. Validating an ObjectID
    2. Getting an individual resource – GET /todos/:id
      1. Taking on the challenge
        1. Challenge step 1 - filling the code
        2. Challenge step 2 - Making the query
        3. challenge step 3 - success path
    3. Testing GET /todos/:id
      1.  Writing test cases for GET/todos/:id
        1. Test 1 - Super test request
        2. Test 2 - Verifying invalid ID
        3. Test 3 - Validating invalid ObjectID
    4. Deploying the API to Heroku
      1. Creating a Heroku app
      2. Heroku logs
    5. Postman environments
      1. Managing Postman environments
        1. Todo App Local environment
        2. Todo App Heroku environment
    6. Deleting a resource – DELETE /todos/:id
      1. Todo.remove method
      2. Todo.findOneAndRemove method
      3. Todo.findByIdAndRemove method
      4. Creating a delete route
    7. Testing DELETE /todos/:id
      1. Test case 1 - should remove a todo
      2. Test case 2 - should return 404 if todo not found
      3. Test case 3 - should return 404 if object id is invalid 
      4. Test case 4 - should return 404 if todo not found
    8. Updating a Resource - PATCH /todos/:id
      1. Installing Lodash library
      2. Testing Todos for the patch call
    9. Testing PATCH /todos/:id
      1. Test 1 -  To complete the incomplete todo
      2. Test 2 - to make complete todo incomplete
    10. Creating a Test database
    11. Summary
  10. Real-Time Web Apps with Socket.io
    1. Creating a new web app project
      1. Setting up our basic app structure
        1. Setting up the index.html file for DOCTYPE
        2. Setting up the server.js file for the public directory
          1. The join method
      2. Configuring basic server setup
      3. Setting up a gitignore file
      4. Making a commit with the current uncommitted files
    2. Adding Socket.io to an app
      1. Setting up Socket.io
        1. Creating a server using the http library
        2. Configuring the server to use Socket.io
        3. Communication between the client and server
          1. The io.on method
          2. Adding a connection event in the client
        4. The disconnect event
    3. Emitting and listening to custom events
      1. Creating custom events inside an application
        1. Moving the JavaScript into a separate file
        2. Adding a newEmail custom event
          1. The emit method
          2. Testing the newEmail event
        3. Adding a createEmail custom event
          1. socket.emit in the developer console
      2. The custom events in the chat app
        1. The newMessage event
    4. Broadcasting events
      1. Wiring up the createMessage listener for all users
        1. Testing the messaging events
        2. Committing and deploying messaging to Heroku
          1. Testing messaging in a Firefox browser using Heroku
      2. Broadcasting events to other users
      3. Emitting two events when a user connects
        1. Greeting an individual user
        2. Broadcasting a new user in the chat
        3. Testing the user connection
    5. Summary
  11. Generating newMessage and newLocationMessage
    1. Message generator and tests
      1. Generating the newMessage object using the utility function
        1. Writing test cases
          1. Adding the test-watch script
          2. Adding the test script
          3. Running the test suite for the message utility
      2. Integrate the utility function into our application
    2. Event acknowledgements
      1. Setting up acknowledgements
      2. Sending an acknowledgement from server to the client
        1. Updating the event emitter
        2. Updating the event listener
    3. The message form and jQuery
      1. Using the jQuery library
      2. Adding the form field in index.html
        1. Setting up the form tag
        2. Adding the text field
        3. Testing the form's rendering
      3. Using jQuery to select element
        1. Adding the selector element to index.js
        2. Testing the update event listener
      4. Rendering incoming messages to the screen
        1. Creating an ordered list to render messages
        2. Using jQuery to create element in index.js
        3. Testing the incoming messages
      5. Making a commit for the message form
    4. Geolocation
      1. Adding the Send Location button to the application
        1. Adding a click listener to the Send Location button
        2. Checking access to the geolocation API
        3. Fetching a user's position
          1. Adding the coordinates object in the users position
          2. Passing coordinates data with the connected users
      2. Rendering clickable link in place of text coordinates
        1. Sorting out the URL structure
        2. Emitting newLoactionMessage
          1. Adding generateLocationMessage in the message.js file
          2. Adding an event listener for newLocationMessage
        3. Adding test case for generateLocationMessage
          1. Adding variables for the test case
          2. Making assertion for generateLocationMessage
          3. Running the test case for generateLocationMessage
    5. Summary
  12. Styling Our Chat Page as a Web App
    1. Styling the chat page
      1. Storing the template styles
        1. Tweaking the structure for alignment
      2. Making user experience improvements
        1. Changing the form submit listener
          1. Updating the input tag
        2. Customizing the Send Location
        3. Updating the button text
    2. Timestamps and formatting with Moment
      1. Timestamps in Node
        1. The Date object
      2. Using Moment for timestamps
        1. The Moment documentation
        2. Formatting date using Moment
        3. The Manipulate section in Moment
    3. Printing message timestamps
      1. Getting the formatted values back from timestamps
        1. Updating the message.js file
        2. Integrating Moment on client
          1. Updating the newMessage property
          2. Updating the newLocationMessage property
    4. Mustache.js
      1. Adding mustache.js to the directory
      2. Creating and rendering template for newMessage
        1. Implementing the Mustache.js rendering method
        2. Getting all the data showing up
        3. Providing a custom structure
          1. Adding the list item tag
          2. Adding the message body tag
      3. Creating template for the newLocation message
        1. Rendering the newLocation template
    5. Autoscrolling
      1. Running a height properties calculation
        1. Creating a new variable to scroll messages to the bottom
        2. Determining the calculation
          1. Taking into account the height of new message
        3. Testing the calculations
        4. Scrolling a user when necessary
      2. Committing the calculation-related changes
    6. Summary
  13. The Join Page and Passing Room Data
    1. Adding a join page
      1. Updating the HTML file
        1. Adding the head tag in the HTML file
        2. Adding the body tag in the HTML file
          1. Adding the form-fields for the chat page
      2. Committing the changes in index.html
    2. Passing room data
      1. Getting data to the server
        1. The params and deparams
        2. Setting up listener in server.js
          1. Defining the isRealString function
          2. Calling the isRealString function in server.js
        3. Adding error handler case in chat.js
        4. Adding test cases for the new validation function
          1. Test case 1 – should reject non-string values
          2. Test case 2 – should reject string with only spaces
          3. Test case 3 – should allow strings with non-space characters
    3. Socket.io rooms
      1. Targeting the specific user
      2. Testing the specific user set up
    4. Summary
  14. ES7 classes
    1. Storing users with ES6 classes – Part I
      1. The ES6 class syntax
        1. Creating the ES6 class for a person
        2. The constructor function
        3. The method function
      2. Adding the users class
        1. Adding the test case for addUser
          1. Adding new instances in the users.test file
          2. Making the assertions for the users call
          3. Running the addUser test case
      3. Adding the removeUser, getUser, and getUserList methods
        1. Adding seed data for the test file
        2. Filling the getUserList
          1. Adding test case for getUserList
        3. Filling the getUser
          1. Test case – should find user
          2. Test case – should not find user
        4. Filling the removeUser method
          1. Test case – should remove a user
          2. Test case – should not remove user
    2. Wiring up user list
      1. Adding People list in the chat room
        1. Adding jQuery to update the DOM
          1. Adding user to the user's list
          2. Adding users with unique ID
        2. Emitting the event to the clients
        3. Testing the users list in the chatroom
      2. Removing users when they leave the chatroom
        1. Updating the users list when someone left the chatroom
        2. Emitting custom message
      3. Rendering the users name to the chatroom
        1. Adding a jQuery to add the users to the list
        2. Rendering the updated People list
        3. Testing the users name in the chatroom
      4. Making a commit for updated users list
    3. Sending messages to room only
      1. Updating the chat.js and server.js files
        1. Emitting event to the individual room
        2. Wiring up createLoactionMessage for individual room
      2. Committing the individual room changes
    4. New feature ideas
    5. Summary
  15. Async/Await Project Setup
    1. Using async/await features in promises
      1. Setting up the getUser project
        1. The array find method
        2. Running the getUser object test
      2. Setting up the getGrades project
        1. Creating grades for the getGrades project
        2. Returning a new promise
      3. Setting up the getStatus project
        1. Resolving the getStatus string
        2. Calculating the average
        3. Returning the template string
    2. Async/await basics
      1. Using the async function
        1. Rejecting an error using the async function
      2. Using the await function
    3. A real-world example
      1. Creating a currency-converter using the async/await function
        1. Exploring APIs for currency exchange rate
        2. Taking advantage of axios inside our application
          1. The getExchangeRate function
          2. The getCountries function
        3. Creating convertCurrencyAlt as the async/await function
    4. Handling errors and awaiting async function
      1. Converting getExchangeRate and getCountries into the async function
      2. Error handling in the async function
        1. Printing an error to the screen
        2. Error handling for the getExchangeRate function
    5. Summary
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Advanced Node.js Development
  • Author(s): Andrew Mead
  • Release date: March 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788393935