Book Description

While there have been quite a few attempts to get JavaScript working as a server-side language, Node.js (frequently just called Node) has been the first environment that's gained any traction. It's now used by companies such as Netflix, Uber and Paypal to power their web apps. Node allows for blazingly fast performance; thanks to its event loop model, common tasks like network connection and database I/O can be executed very quickly indeed.

From a beginner's point of view, one of Node's obvious advantages is that it uses JavaScript, a ubiquitous language that many developers are comfortable with. If you can write JavaScript for the client-side, writing server-side applications with Node should not be too much of a stretch for you.

This book offers aselection of beginner-level tutorials to privide you with an introduction to Node and its related technologies, and get you under way writing your first Node applications. It contains:

  1. What Is Node and When Should I Use It?
  2. Build a Simple Page Counter Service with Node.js
  3. Understanding module.exports and exports in Node.js
  4. Forms, File Uploads and Security with Node.js and Express
  5. Working with Databases in Node
  6. How to Build and Structure a Node.js MVC Application
  7. Local Authentication Using Passport in Node.js
  8. How to Debug a Node App
  9. Node Testing for Beginners
  10. How to Use SSL/TLS with Node.js
  11. Configuring NGINX and SSL with Node.js
  12. Using Docker for Node.js Development

Table of Contents

  1. Your First Week With Node.js, 2nd Edition
  2. Notice of Rights
  3. Notice of Liability
  4. Trademark Notice
  5. About SitePoint
  6. About Craig Buckler
  7. Preface
    1. Who Should Read This Book?
    2. Conventions Used
      1. Code Samples
      2. Tips, Notes, and Warnings
    3. Supplementary Materials
  8. Chapter 1: What Is Node and When Should I Use It?
    1. by James Hibbard
    2. What Is Node.js?
      1. Node Is Built on Google Chrome’s V8 JavaScript Engine
    3. How Do I Install Node.js?
      1. Node Binaries vs Version Manager
      2. “Hello, World!” the Node.js Way
      3. Node.js Has Excellent Support for Modern JavaScript
    4. Introducing npm, the JavaScript Package Manager
      1. Installing a Package Globally
      2. Installing a Package Locally
      3. Working with the package.json File
    5. What Is Node.js Used For?
    6. Node.js Lets Us Run JavaScript on the Server
      1. The Node.js Execution Model
      2. Are There Any Downsides?
      3. “Hello, World!”—Server Version
    7. What Kind of Apps Is Node.js Suited To?
    8. What Are the Advantages of Node.js?
    9. Other Uses of Node
    10. Conclusion
  9. Chapter 2: Build a Simple Page Counter Service with Node.js
    1. by Craig Buckler
    2. Prerequisites
    3. Project Overview
    4. Page Hit v1.0: In-memory Storage
      1. Step 0 (optional): Source Control
      2. Step 1: Initialization
      3. Step 2: Main File (index.js)
      4. Step 3: Additional Modules
      5. Step 4: Test Page
    5. Page Hit v2.0: File Storage
      1. Node.js File System API
      2. Step 1: File Utilities
      3. Step 2: Modify lib/pagehit.js
      4. Step 3: Test
    6. Job Done?
  10. Chapter 3: Understanding module.exports and exports in Node.js
    1. by James Hibbard
    2. Different Module Formats
    3. Requiring a Module
    4. Creating and Exporting a Module
      1. Exporting Multiple Methods and Values
      2. Variations in Syntax
    5. Exporting a Default Value
    6. What’s the Difference Between module.exports and exports?
      1. So Which One Should I use?
    7. Conclusion
  11. Chapter 4: Forms, File Uploads and Security with Node.js and Express
    1. by Mark Brown and James Hibbard
    2. Setup
    3. Displaying the Form
    4. Form Submission
    5. Validation and Sanitization
      1. Validation
      2. Sanitization
    6. The Valid Form
      1. Sending Email with Node
    7. Security considerations
      1. TLS over HTTPS
      2. Wear Your Helmet
      3. Cross-site Request Forgery (CSRF)
      4. Cross-site Scripting (XSS)
    8. File Uploads
      1. Populating File Inputs
      2. Uploading Files with Node
    9. Thanks for Reading
  12. Chapter 5: Working with Databases in Node
    1. by Craig Buckler
    2. Evaluate CMS Templates and Plugins
    3. Reduce Client-side Code
    4. Optimize JavaScript Code
      1. Use JavaScript Sparingly
      2. Avoid Long-running Tasks
      3. Bind Events Sparingly
      4. Analyze Modified Code
    5. Modify the DOM Effectively
      1. Cache Regularly Used Nodes
      2. Minimize Reflows
      3. Batch-update Styles
      4. Batch-update Elements
      5. Use requestAnimationFrame
    6. Consider Progressive Rendering
    7. Use Server-side Rendering
    8. Do You Need a JavaScript or CSS Framework?
    9. Use a Static Site Generator
    10. Use a Build System
    11. Use Progressive Enhancement
    12. Adopt a Performance Budget
    13. Create a Style Guide
    14. Simplify and Streamline
    15. Learn to Love the Web
  13. Chapter 6: How to Build and Structure a Node.js MVC Application
    1. by Nilson Jacques and James Kolce
    2. What is MVC?
    3. Laying out the Foundation
    4. Storing Our Settings
    5. Defining the Routes
    6. Building the Models
      1. Setting up the Database
      2. Creating Our Note Model
      3. Synchronizing the Database
    7. Building the Controllers
      1. The Home Controller
      2. Boilerplate of the Note Controller
      3. The create Function
      4. The read Function
      5. The update Function
      6. The delete Function
      7. Using the Note Controller in Our Routes
    8. Building the Views
      1. The Note Component
      2. The Base Layout
      3. The Home View
      4. The Note View
      5. The JavaScript on the Client
      6. Adding Support for Views on the Server
      7. Setting the Home View
      8. Setting the Note View: create Function
      9. Setting the Note View: read Function
      10. Setting the Note View: update Function
    9. Serving Static Files
    10. Conclusion
  14. Chapter 7: Local Authentication Using Passport in Node.js
    1. by Paul Orac
    2. Prerequisites
    3. Authentication Strategies: Session vs JWT
    4. Creating the Project
    5. Setting up Express
    6. Setting up Passport
    7. Creating a MongoDB Data Store
    8. Connecting Mongo to Node with Mongoose
    9. Implementing Local Authentication
    10. Routes
    11. The Client
    12. Next Steps
    13. Conclusion
  15. Chapter 8: How to Debug a Node App
    1. by Craig Buckler
    2. What is Debugging?
      1. Terminology
    3. How to Avoid Bugs
      1. Use a Good Code Editor
      2. Use a Code Linter
      3. Use Source Control
      4. Adopt an Issue-tracking System
      5. Use Test-driven Development
      6. Step Away
    4. Node.js Debugging: Environment Variables
    5. Node.js Debugging: Command Line Options
    6. Console Debugging
    7. Node.js util.debuglog
    8. Debugging with Log Modules
    9. Node.js V8 Inspector
    10. Node.js Debugging with Chrome
    11. Node.js Debugging with VS Code
      1. Advanced Debugging Configuration
    12. Other Node.js Debugging Tools
    13. Get Debugging!
  16. Chapter 9: Node Testing for Beginners
    1. by M. David Green
    2. What Testing Is
    3. Basic Assertion Testing Built In
      1. The Existential Assertion
      2. Asserting Equality
      3. Testing Edge Cases
    4. Structuring Your Tests
      1. Test Cases
      2. Test Suites
      3. Test Harnesses
      4. Fixtures
      5. Test Doubles
    5. Frameworks for Automated Testing
      1. Jasmine is the Oldest
      2. Jest is the Most Popular
      3. Mocha/Chai is the Most Comprehensive
      4. Cypress is the Newest
    6. Get Started Now
  17. Chapter 10: How to Use SSL/TLS with Node.js
    1. by Florian Rappl and Almir Bijedic
    2. HTTPS Everywhere
    3. Basic Communication
    4. HTTPS
    5. Generating Certificates
      1. Certbot
    6. Tighetning It Up
      1. HTTP Strict Transport Security
      2. Diffie–Hellman Strong(er) Parameters
    7. Conclusion
  18. Chapter 11: Configuring NGINX and SSL with Node.js
    1. by Nilson Jacques
    2. Installing NGINX
      1. Installation on Linux
      2. Installation on macOS
      3. Installation on Windows
    3. Setting Up a Node.js Server
    4. Configuring NGINX
    5. Setting Up SSL
    6. Bonus: Serving Static Assets
    7. Conclusion
  19. Chapter 12: Using Docker for Node.js Development
    1. by Craig Buckler
    2. “But it Works on My Machine!”
    3. Virtual Machines
    4. Docker Overview
    5. Docker Concepts and Terminology
      1. Images
      2. Containers
      3. Volumes
      4. Networks
      5. Docker Compose
      6. Development and Production Environments
      7. How to Install Docker
      8. Permission Errors
    6. How to Launch a Database Container
    7. How to Launch a Node.js Container
    8. Using Docker Compose During Development
    9. Using Docker Compose in Production
    10. Cleaning Up
    11. Next Steps