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

Node.js Web Development - Third Edition

Book Description

Create real-time server-side applications with this practical, step-by-step guide

About This Book

  • Learn about server-side JavaScript with Node.js and Node modules through the most up-to-date book on Node.js web development
  • Understand website development both with and without the Connect/Express web application framework
  • Develop both HTTP server and client applications

Who This Book Is For

This book is for anybody looking for an alternative to the "P" languages (Perl, PHP, and Python), or anyone looking for a new paradigm of server-side application development. You should have at least a rudimentary understanding of JavaScript and web application development.

What You Will Learn

  • Install and use Node.js for both development and deployment
  • Use the Express application framework
  • Configure Bootstrap for mobile-first theming
  • Use data storage engines such as MySQL, SQLITE3, and MongoDB
  • Understand user authentication methods, including OAuth, with third-party services
  • Deploy Node.js to live servers, including microservice development with Docker
  • Perform unit testing with Mocha
  • Perform functional testing of the web application with CasperJS

In Detail

Node.js is a server-side JavaScript platform using an event driven, non-blocking I/O model allowing users to build fast and scalable data-intensive applications running in real time. Node.js Web Development shows JavaScript is not just for browser-side applications. It can be used for server-side web application development, real-time applications, microservices, and much more.

This book gives you an excellent starting point, bringing you straight to the heart of developing web applications with Node.js. You will progress from a rudimentary knowledge of JavaScript and server-side development to being able to create and maintain your own Node.js application. With this book you'll learn how to use the HTTP Server and Client objects, data storage with both SQL and MongoDB databases, real-time applications with Socket.IO, mobile-first theming with Bootstrap, microservice deployment with Docker, authenticating against third-party services using OAuth, and much more.

Style and Approach

This book is a practical guide for anyone looking to develop striking and robust web applications using Node.js.

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 code file.

Table of Contents

  1. Node.js Web Development Third Edition
    1. Table of Contents
    2. Node.js Web Development Third Edition
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. 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
    8. 1. 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. The Node.js is stronger for having survived a major schism and hostile fork
        7. Performance and utilization
        8. Is Node.js a cancerous scalability disaster?
        9. Server utilization, the bottom line, and green web hosting
        10. Node.js, the microservice architecture, and easily testable systems
        11. Node.js and the Twelve-Factor app model
      3. Summary
    9. 2. Setting up Node.js
      1. System requirements
      2. Installing Node.js using package managers
        1. Installing on Mac OS X with MacPorts
        2. Installing on Mac OS X with Homebrew
        3. Installing on Linux, *BSD, or Windows from package management systems
        4. Installing the Node.js distribution from nodejs.org
      3. Installing from source on POSIX-like systems
        1. Installing prerequisites
        2. Native code modules and node-gyp
      4. Installing developer tools on Mac OS X
        1. Installing from source for all POSIX-like systems
        2. Installing development instances with nvm
      5. Node.js versions policy and what to use
      6. Running and testing commands
        1. Node.js's command-line tools
        2. Running a simple script with Node.js
        3. Launching a server with Node.js
      7. NPM – the Node.js package manager
      8. Node.js and ECMAScript 6 (ES-2015, ES-2016, and so on)
        1. Using Babel to use experimental JavaScript features
      9. Summary
    10. 3. Node.js Modules
      1. Defining a module
        1. Node.js module format
        2. File modules
          1. Demonstrating module-level encapsulation
        3. Directories as modules
      2. Node.js's algorithm for require (module)
        1. Module identifiers and path names
        2. An example application directory structure
      3. npm – the Node.js package management system
        1. The npm package format
        2. Finding npm packages
        3. Other npm commands
          1. Installing an npm package
          2. Initializing a new npm package
          3. Maintaining package dependencies with npm
          4. Fixing bugs by updating package dependencies
          5. Declaring Node.js version compatibility
          6. Updating outdated packages you've installed
          7. Installing packages from outside the npm repository
          8. Publishing an npm package
        4. Package version numbers
      4. A quick note about CommonJS
      5. Summary
    11. 4. HTTP Servers and Clients – A Web Application's First Steps
      1. Sending and receiving events with EventEmitters
        1. The EventEmitter theory
      2. HTTP server applications
      3. ES-2015 multiline and template strings
      4. HTTP Sniffer – listening to the HTTP conversation
      5. Web application frameworks
      6. Getting started with Express
        1. Walking through the default Express application
        2. The Express middleware
        3. Middleware and request paths
        4. 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
    12. 5. Your First Express Application
      1. ES-2015 Promises and Express router functions
        1. Promises and error handling
        2. Flattening our asynchronous code
        3. Additional tools
      2. Express and the MVC paradigm
      3. Creating the Notes application
        1. Your first 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
    13. 6. 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
      4. Mobile-first design for the Notes application
        1. Laying the Bootstrap grid foundation
        2. Improving the notes list on the front page
        3. Breadcrumbs for the page header
        4. Cleaning up the add/edit note form
      5. Building a customized Bootstrap
        1. Bootstrap customizers
      6. Summary
    14. 7. 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
      3. Storing notes in the filesystem
        1. Storing notes with the LevelUP data store
        2. Storing notes in SQL with SQLite3
          1. SQLite3 database scheme
          2. SQLite3 model code
          3. Running Notes with SQLite3
        3. 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
        4. Storing notes in MongoDB
          1. MongoDB model for the Notes application
          2. Running the Notes application with MongoDB
      4. Summary
    15. 8. 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.js
          5. Login/logout changes required in routes/notes.js
          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. The Notes application stack
      3. Summary
    16. 9. Dynamic Interaction between Client and Server with Socket.IO
      1. Introducing Socket.IO
      2. Initializing Socket.IO with Express
      3. Real time updates on the Notes home page
        1. The Notes model as an EventEmitter class
        2. Real-time changes in the Notes home page
          1. Changing the home page template
          2. Running Notes with real-time home page 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
    17. 10. Deploying Node.js Applications
      1. Notes application architecture
      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
        4. Setting up PM2 to manage Node.js processes
          1. Twitter support for the hosted Notes app
      3. Node.js microservice deployment with Docker
        1. Installing Docker on your laptop
          1. Starting Docker using Docker Toolbox and Docker Machine
          2. Starting Docker with Docker for Windows/Mac
          3. Kicking the tires of Docker
        2. Creating the AuthNet for the User Authentication service
          1. MySQL for the Authentication service
          2. Dockerizing the Authentication service
          3. Putting Authnet together
        3. Creating FrontNet for the Notes application
          1. MySQL for the Notes application
          2. Dockerizing the Notes application
          3. Putting FrontNet together
          4. Configuring remote access on Docker for Windows or Mac
          5. Configuring remote access in VirtualBox on Docker toolbox
          6. Exploring the Docker Toolbox VirtualBoxMachine
          7. Controlling the location of MySQL data volumes
        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
    18. 11. Unit Testing
      1. Testing asynchronous code
      2. Assert – the simplest testing methodology
      3. Testing a 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
      4. Using Docker to manage test database servers
        1. Docker Compose to orchestrate test infrastructure
        2. Package.json scripts for Dockerized test infrastructure
        3. Executing tests under Docker Compose
      5. Testing REST backend services
      6. Frontend headless browser testing with CasperJS
        1. Setup
        2. Improving testability in Notes UI
        3. CasperJS test script for Notes
        4. Running the UI test with CasperJS
      7. Summary
    19. Index