You are previewing Professional Node.js: Building Javascript Based Scalable Software.

Professional Node.js: Building Javascript Based Scalable Software

  1. Cover
  2. Contents
  3. Part I: Introduction and Setup
    1. Chapter 1: Installing Node
      1. Installing Node on Windows
      2. Installing on Mac OS X
      3. Installing Node Using the Source Code
      4. Setting Up and Using Node Package Manager
      5. Summary
    2. Chapter 2: Introducing Node
      1. Introducing the Event-Driven Programming Style
      2. How Node and JavaScript Make Writing Asynchronous Applications Easier
      3. Summary
  4. Part II: Node Core API Basics
    1. Chapter 3: Loading Modules
      1. Understanding How Node Loads Modules
      2. Exporting a Module
      3. Summary
    2. Chapter 4: Using Buffers to Manipulate, Encode, and Decode Binary Data
      1. Creating a Buffer
      2. Getting and Setting Bytes in a Buffer
      3. Slicing a Buffer
      4. Copying a Buffer
      5. Decoding a Buffer
      6. Summary
    3. Chapter 5: Using the Event Emitter Pattern to Simplify Event Binding
      1. Understanding the Standard Callback Pattern
      2. Understanding the Event Emitter Pattern
      3. Understanding Event Types
      4. Using the Event Emitter API
      5. Creating an Event Emitter
      6. Summary
    4. Chapter 6: Scheduling the Execution of Functions Using Timers
      1. Using setTimeout to Defer the Execution of a Function
      2. Using clearTimeout to Cancel the Execution of a Function
      3. Scheduling and Canceling the Repetitive Execution of a Function
      4. Using process.nextTick to Defer the Execution of a Function Until the Next Event Loop Iteration
      5. Blocking the Event Loop
      6. Escaping the Event Loop
      7. Using setTimeout Instead of setInterval to Force Serialization
      8. Summary
  5. Part III: Files, Processes, Streams, and Networking
    1. Chapter 7: Querying, Reading From, and Writing to Files
      1. Manipulating File Paths
      2. Introducing the fs Module
      3. Opening a File
      4. Reading from a File
      5. Summary
    2. Chapter 8: Creating and Controlling External Processes
      1. Executing External Commands
      2. Spawning Child Processes
      3. Signaling and Killing Processes
      4. Summary
    3. Chapter 9: Reading and Writing Streams of Data
      1. Using a Readable Stream
      2. Using Writable Streams
      3. Considering Some Stream Examples
      4. Avoiding the Slow Client Problem and Saving Your Server
      5. Summary
    4. Chapter 10: Building TCP Servers
      1. Creating a TCP Server
      2. Building a Simple TCP Chat Server
      3. Summary
    5. Chapter 11: Building HTTP Servers
      1. Understanding the http.ServerRequest Object
      2. Understanding the http.ServerResponse Object
      3. Streaming HTTP Chunked Responses
      4. Shutting Down the Server
      5. Example 1: Building a Server that Serves Static Files
      6. Example 2: Making Use of HTTP Chunked Responses and Timers
      7. Summary
    6. Chapter 12: Building a TCP Client
      1. Connecting to a Server
      2. Sending and Receiving Data
      3. Ending the Connection
      4. Handling Errors
      5. Building an Example Command-Line TCP Client
      6. Summary
    7. Chapter 13: Making HTTP Requests
      1. Making GET Requests
      2. Using Other HTTP Verbs
      3. Pooling Sockets Using http.Agent
      4. Using a Third-Party Request Module to Simplify HTTP Requests
      5. Summary
    8. Chapter 14: Using Datagrams (UDP)
      1. Understanding UDP
      2. Understanding the Uses of UDP
      3. Building a Datagram Server
      4. Creating a Simple Datagram Echo Server
      5. Building a Datagram Client
      6. Creating a Simple Datagram Command-Line Client
      7. Understanding and Using Datagram Multicast
      8. Summary
    9. Chapter 15: Securing Your TCP Server With TLS/SSL
      1. Understanding Private and Public Keys
      2. Building a TLS Server
      3. Building a TLS Client
      4. Building Some Examples
      5. Summary
    10. Chapter 16: Securing Your HTTP Server With HTTPS
      1. Building a Secure HTTP Server
      2. Creating an HTTPS Client
      3. Summary
  6. Part IV: Building and Debugging Modules and Applications
    1. Chapter 17: Testing Modules and Applications
      1. Using a Test Runner
      2. Using an Assertion Testing Module
      3. Testing Your Asynchronous Module
      4. Summary
    2. Chapter 18: Debugging Modules and Applications
      1. Using console.log
      2. Using Node’s Built-in Debugger
      3. Using Node Inspector
      4. Summary
    3. Chapter 19: Controlling the Callback Flow
      1. Understanding the Boomerang Effect
      2. Avoiding the Boomerang Effect by Declaring Functions
      3. Using the async Flow Control Library
      4. Summary
  7. Part V: Building Web Applications
    1. Chapter 20: Building and Using HTTP Middleware
      1. Understanding the Connect HTTP Middleware Framework
      2. Building Your Own HTTP Middleware
      3. Using the HTTP Middleware Bundled in Connect
      4. Summary
    2. Chapter 21: Making a Web Application Using Express.js
      1. Initializing Your Express.js Application
      2. Setting Up Middleware in Your Application
      3. Routing Requests
      4. Summary
    3. Chapter 22: Making Universal Real-Time Web Applications Using Socket.io
      1. Understanding How WebSockets Work
      2. Using Socket.IO to Build WebSocket Applications
      3. Summary
  8. Part VI: Connecting to Databases
    1. Chapter 23: Connecting to MYSQL Using node-mysql
      1. Using a Library to Connect to and Communicate with a MySQL Database
      2. Adding Data to the Database with Security Concerns in Mind
      3. Reading Data Efficiently
      4. Summary
    2. Chapter 24: Connecting to CouchDB Using Nano
      1. Installing Nano
      2. Connecting and Creating a Database
      3. Storing Documents
      4. Creating and Using CouchDB Views
      5. Attaching Files to a CouchDB Document
      6. Summary
    3. Chapter 25: Connecting to MongoDB Using MongooSE
      1. Installing Mongoose
      2. Understanding How Mongoose Uses Models to Encapsulate Database Access
      3. Connecting to MongoDB
      4. Defining a Schema
      5. Defining a Model
      6. Summary
  9. Introduction
  10. Advertisements
O'Reilly logo

Chapter 2

Introducing Node

WHAT’S IN THIS CHAPTER?

  • What is the event-driven programming style and what are the advantages of using it
  • How Node.js and JavaScript make event-driven programming easy

Traditional programming does I/O the same way as it does local function calls: Processing cannot continue until an operation finishes. This programming model of blocking when doing I/O operations derives from the early days of time-sharing systems in which each process corresponded to one human user. The purpose was to isolate users from one another. In those systems, a user would typically need to finish one operation before deciding what the next operation would be. But with widespread use of computer networks and the Internet, this model of “one user, one process” did not scale well. Managing many processes places a big burden on the operating system — in memory and context switching costs — and the performance of these tasks starts to decay after a certain number is reached.

Multi-threading is one alternative to this programming model. A thread is a kind of lightweight process that shares memory with every other thread within the same process. Threads were created as an ad hoc extension of the previous model to accommodate several concurrent threads of execution. When one thread is waiting for an I/O operation, another thread can take over the CPU. When the I/O operation finishes, that thread can wake up, which means the thread that was running can be interrupted and eventually be ...

The best content for your career. Discover unlimited learning on demand for around $1/day.