Hands-on Node.js

Retired

This book is no longer available for sale.

Hands-on Node.js

About the Book

In this e-book we will analyze what makes Node a different proposal from all that is out there, why you should use it, and how to get started. It starts with an overview but quickly dives into the code, module by module. By the end of this book you should be able to build your own Node service producers and consumers, and also feel comfortable around the Node API and conventions.

By the end of it you should be able to build your own Node modules, as well as test and debug them.

About the Author

Pedro Teixeira
Pedro Teixeira

Pedro Teixeira is a geek, programmer, freelancer, and entrepreneur. He's the author of some Node.js modules, the Node Tuts screencast show, and the Hands-on Node.js e-book and overall fervent proclaimer of the Node.js creed.

He's the co-founder and Partner of TheNodeFirm, and also the organizer of the Lisbon JavaScript Conference.

Table of Contents

  • Introduction
    • Why the sudden, exponential popularity?
    • What does this book cover?
    • What does this book not cover?
    • Prerequisites
    • Exercises
    • Source code
    • Where will this book lead you?
  • Why?
    • Why the event loop?
      • Solution 1: Create more call stacks
      • Solution 2: Use event-driven I/O
    • Why JavaScript?
      • How I Learned to Stop Fearing and Love JavaScript
        • Function Declaration Styles
        • Functions are first-class objects
      • JSHint
      • JavaScript versions
    • References
  • Starting up
    • Install Node
  • Understanding
    • Understanding the Node event loop
      • An event-queue processing loop
      • Callbacks that will generate events
      • Don’t block!
  • Modules and NPM
    • Modules
      • How Node resolves a module path
        • Core modules
        • Modules with complete or relative path
        • As a file
        • As a directory
        • As an installed module
    • NPM - Node Package Manager
      • Global vs. Local
      • NPM commands
        • npm ls [filter]
        • npm install package[@filters]
        • npm rm package_name[@version] [package_name[@version] …]
        • npm view [@] [[.]…]
    • The Package.json Manifest
  • Utilities
    • console
    • util
  • Buffers
    • Slice a buffer
    • Copy a buffer
    • Buffer Exercises
      • Exercise 1
      • Exercise 2
      • Exercise 3
  • Event Emitter
    • Creating an Event Emitter
    • Event Emitter Exercises
      • Exercise 1
      • Exercise 2
  • Timers
    • setTimeout
    • clearTimeout
    • setInterval
    • clearInterval
    • setImmediate
      • Escaping the event loop
      • A note on tail recursion
  • Low-level file-system
    • fs.stat and fs.fstat
    • Open a file
    • Read from a file
    • Write into a file
      • Close Your files
    • File-system Exercises
      • Exercise 1 - get the size of a file
      • Exercise 2 - read a chunk from a file
      • Exercise 3 - read two chunks from a file
      • Exercise 4 - Overwrite a file
      • Exercise 5 - append to a file
      • Exercise 6 - change the content of a file
  • HTTP
    • HTTP Server
      • The http.ServerRequest object
        • req.url
        • req.method
        • req.headers
        • The http.ServerResponse object
          • Write a header
          • Change or set a header
          • Remove a header
          • Write a piece of the response body
    • HTTP Client
      •  
        • http.get()
      • http.request()
    • HTTP Exercises
      • Exercise 1
      • Exercise 2
      • Exercise 3
      • Exercise 4
  • Streams
    • ReadStream
      • Wait for data
      • Know when it ends
      • Pause it
      • Resume it
    • WriteStream
      • Write to it
      • Wait for it to drain
    • Some stream examples
      • Filesystem streams
      • Network streams
    • The Slow Client Problem and Back-pressure
      • What can we do?
      • Pipe
  • TCP
    • Write a string or a buffer
    • end
    • …and all the other methods
    • Idle sockets
    • Keep-alive
    • Delay or no delay
    • server.close()
    • Listening
    • TCP client
    • Error handling
    • TCP Exercises
      • Exercise 1
      • Exercise 2
  • Datagrams (UDP)
    • Datagram server
    • Datagram client
    • Datagram Multicast
      • Receiving multicast messages
      • Sending multicast messages
      • What can be the datagram maximum size?
    • UDP Exercises
      • Exercise 1
  • Child processes
    • Executing commands
    • Spawning processes
    • Killing processes
    • Child Processes Exercises
      • Exercise 1
  • Streaming HTTP chunked responses
    •  
      • A streaming example
    • Streaming Exercises
      • Exercise 1
  • TLS / SSL
    • Public / private keys
      • Private key
      • Public key
    • TLS Client
    • TLS Server
      • Verification
    • TLS Exercises
      • Exercise 1
      • Exercise 2
      • Exercise 3
      • Exercise 4
      • Exercise 5
  • HTTPS
    • HTTPS Server
    • HTTPS Client
  • Making modules
    • CommonJS modules
    • One file module
    • An aggregating module
    • A pseudo-class
    • A pseudo-class that inherits
    • node_modules and npm bundle
      • Bundling
  • Debugging
    • console.log
    • Node built-in debugger
    • Node Inspector
    • Live edit
  • Automated Unit Testing
    • A test runner
    • Assertion testing module
      • should.js
        •  
          • Assert truthfulness:
          • or untruthfulness:
          • = true
          • = false
          • emptiness
          • equality
          • equal (strict equality)
          • assert numeric range (inclusive) with within
          • test numeric value is above given value:
          • test numeric value is below given value:
          • matching regular expressions
          • test length
          • substring inclusion
          • assert typeof
          • property existence
          • array containment
          • own object keys
          • responds to, asserting that a given property is a function:
    • Putting it all together
  • Callback flow
    • The boomerang effect
    • Using caolan/async
      • Collections
        • Parallel Iterations
        • async.forEach
        • async.map
        • async.forEachLimit
        • async.filter
        • async.reject
        • async.reduce
        • async.detect
        • async.some
        • async.every
      • Flow Control
        • async.series
        • async.parallel
        • async.whilst
        • async.until
        • async.waterfall
        • async.queue
  • Appendix - Exercise Results
    • Chapter: Buffers
      • Exercise 1
        • One Solution:
      • Exercise 2
        • One Solution:
      • Exercise 3
        • One Solution:
    • Chapter: Event Emitter
      • Exercise 1
        • One Solution:
      • Exercise 2
        • One Solution:
    • Chapter: Low-level File System
      • Exercise 1 - get the size of a file
        • One Solution:
      • Exercise 2 - read a chunk from a file
        • One Solution:
      • Exercise 3 - read two chunks from a file
        • One Solution:
      • Exercise 4 - Overwrite a file
        • One Solution:
      • Exercise 5 - append to a file
        • One Solution:
      • Exercise 6 - change the content of a file
        • One Solution:
    • Chapter: HTTP
      • Exercise 1
        • One Solution:
      • Exercise 2
        • One Solution:
      • Exercise 3
        • One Solution:
      • Exercise 4
        • One Solution:
    • Chapter: Child processes
      • Exercise 1
        • One Solution:
    • Chapter: Streaming HTTP Chunked responses
      • Exercise 1
        • One Solution:
    • Chapter: UDP
      • Exercise 1
        • One Solution:
    • Chapter: TCP
      • Exercise 1
        • One Solution:
      • Exercise 2
        • One Solution:
    • Chapter: SSL / TLS
      • Exercise 1
        • One Solution:
      • Exercise 2
        • One Solution:
      • Exercise 3
        • One Solution:
      • Exercise 4
        • One Solution:
      • Exercise 5
        • One Solution:

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub