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

Mastering Node.js

Book Description

Using the JavaScript language you already know, this book will show you how to easily build scalable network software with Node.js. You’ll learn through practical examples and clear explanations every step of the way.

  • Master the latest techniques for building real-time, big data applications, integrating Facebook, Twitter, and other network services
  • Tame asynchronous programming, the event loop, and parallel data processing
  • Use the Express and Path frameworks to speed up development and deliver scalable, higher quality software more quickly

In Detail

Node.js is a modern development stack focused on providing an easy way to build scalable network software. Backed by a growing number of large companies and a rapidly increasing developer base, Node is revolutionizing the way that software is being built today. Powered by Google’s V8 engine and built out of C++ modules, this is a JavaScript environment for the enterprise.

Mastering Node.js will take the reader deep into this exciting development environment. Beginning with a comprehensive breakdown of its innovative non-blocking evented design, Node’s structure is explained in detail, laying out how its blazingly fast I/O performance simplifies the creation of fast servers, scalable architectures, and responsive web applications.

Mastering Node.js takes you through a concise yet thorough tour of Node's innovative evented non-blocking design, showing you how to build professional applications with the help of detailed examples.

Learn how to integrate your applications with Facebook and Twitter, Amazon and Google, creating social apps and programs reaching thousands of collaborators on the cloud. See how the Express and Path frameworks make the creation of professional web applications painless. Set up one, two, or an entire server cluster with just a few lines of code, ready to scale as soon as you’re ready to launch. Move data seamlessly between databases and file systems, between clients, and across network protocols, using a beautifully designed, consistent, and predictable set of tools.

Mastering Node.js contains all of the examples and explanations you’ll need to build applications in a short amount of time and at a low cost, running on a scale and speed that would have been nearly impossible just a few years ago.

Table of Contents

  1. Mastering Node.js
    1. Table of Contents
    2. Mastering Node.js
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
      2. Why Subscribe?
        1. Free Access for Packt account holders
    8. 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. Errata
        2. Piracy
        3. Questions
    9. 1. Understanding the Node Environment
      1. Extending JavaScript
        1. Events
        2. Modularity
        3. The Network
      2. V8
        1. Memory and other limits
        2. Harmony
      3. The process object
      4. The Read-Eval-Print Loop and executing a Node program
      5. Summary
    10. 2. Understanding Asynchronous Event-Driven Programming
      1. Broadcasting events
        1. Collaboration
        2. Queueing
      2. Listening for events
        1. Signals
        2. Forks
        3. File events
        4. Deferred execution
          1. process.nextTick
            1. setImmediate
      3. Timers
        1. setTimeout
        2. setInterval
        3. unref and ref
      4. Understanding the event loop
        1. Four sources of truth
      5. Callbacks and errors
        1. Conventions
        2. Know your errors
        3. Building pyramids
        4. Considerations
      6. Listening for file changes
      7. Summary
    11. 3. Streaming Data Across Nodes and Clients
      1. Exploring streams
        1. Implementing readable streams
          1. Pushing and pulling
        2. Writable streams
        3. Duplex streams
        4. Transforming streams
        5. Using PassThrough streams
      2. Creating an HTTP server
        1. Making HTTP requests
        2. Proxying and tunneling
      3. HTTPS, TLS (SSL), and securing your server
        1. Creating a self-signed certificate for development
        2. Installing a real SSL certificate
      4. The request object
        1. The URL module
        2. The Querystring module
      5. Working with headers
        1. Using cookies
        2. Understanding content types
        3. Handling favicon requests
      6. Handling POST data
      7. Creating and streaming images with Node
        1. Creating, caching, and sending a PNG representation
      8. Summary
    12. 4. Using Node to Access the Filesystem
      1. Directories, and iterating over files and folders
        1. Types of files
        2. File paths
        3. File attributes
        4. Opening and closing files
          1. fs.open(path, flags, [mode], callback)
          2. fs.close(fd, callback)
        5. File operations
          1. fs.rename(oldName, newName, callback)
          2. fs.truncate(path, len, callback)
          3. fs.ftruncate(fd, len, callback)
          4. fs.chown(path, uid, gid, callback)
          5. fs.fchown(fd, uid, gid, callback)
          6. fs.lchown(path, uid, gid, callback)
          7. fs.chmod(path, mode, callback)
          8. fs.fchmod(fd, mode, callback)
          9. fs.lchmod(path, mode, callback)
          10. fs.link(srcPath, dstPath, callback)
          11. fs.symlink(srcPath, dstPath, [type], callback)
          12. fs.readlink(path, callback)
          13. fs.realpath(path, [cache], callback)
          14. fs.unlink(path, callback)
          15. fs.rmdir(path, callback)
          16. fs.mkdir(path, [mode], callback)
          17. fs.exists(path, callback)
          18. fs.fsync(fd, callback)
        6. Synchronicity
        7. Moving through directories
      2. Reading from a file
        1. Reading byte by byte
          1. fs.read(fd, buffer, offset, length, position, callback)
        2. Fetching an entire file at once
          1. fs.readFile(path, [options], callback)
        3. Creating a readable stream
          1. fs.createReadStream(path, [options])
        4. Reading a file line by line
          1. The Readline module
      3. Writing to a file
        1. Writing byte by byte
          1. fs.write(fd, buffer, offset, length, position, callback)
        2. Writing large chunks of data
          1. fs.writeFile(path, data, [options], callback)
          2. fs.appendFile(path, data, [options], callback)
        3. Creating a writable stream
          1. fs.createWriteStream(path, [options])
        4. Caveats
      4. Serving static files
        1. Redirecting requests
          1. Location
          2. Content-Location
        2. Implementing resource caching
      5. Handling file uploads
        1. Putting it all together
      6. Summary
    13. 5. Managing Many Simultaneous Client Connections
      1. Understanding concurrency
        1. Concurrency is not parallelism
      2. Routing requests
        1. Understanding routes
        2. Using Express to route requests
      3. Using Redis for tracking client state
        1. Storing user data
      4. Handling sessions
        1. Cookies and client state
        2. A simple poll
        3. Centralizing states
      5. Authenticating connections
        1. Basic authentication
        2. Handshaking
      6. Summary
      7. Further reading
    14. 6. Creating Real-time Applications
      1. Introducing AJAX
        1. Responding to calls
        2. Creating a stock ticker
      2. Bidirectional communication with Socket.IO
        1. Using the WebSocket API
        2. Socket.IO
        3. Drawing collaboratively
      3. Listening for Server Sent Events
        1. Using the EventSource API
        2. The EventSource stream protocol
        3. Asking questions and getting answers
      4. Building a collaborative document editing application
      5. Summary
    15. 7. Utilizing Multiple Processes
      1. Node's single-threaded model
        1. The benefits of single-threaded programming
        2. Multithreading is already native and transparent
      2. Creating child processes
        1. Spawning processes
        2. Forking processes
        3. Buffering process output
      3. Communicating with your child
        1. Sending messages to children
      4. Parsing a file using multiple processes
        1. Using the cluster module
        2. Cluster events
        3. Worker object properties
        4. Worker events
      5. Real-time activity updates of multiple worker results
      6. Summary
    16. 8. Scaling Your Application
      1. When to scale?
        1. Network latency
        2. Hot CPUs
        3. Socket usage
        4. Many file descriptors
        5. Data creep
        6. Tools for monitoring servers
      2. Running multiple Node servers
        1. Forward and reverse proxies
        2. Nginx as a proxy
        3. Using HTTP Proxy
      3. Message queues – RabbitMQ
        1. Types of exchanges
      4. Using Node's UDP module
        1. UDP multicasting with Node
      5. Using Amazon Web Services in your application
        1. Authenticating
        2. Errors
        3. Using S3 to store files
          1. Working with buckets
          2. Working with objects
          3. Using AWS with a Node server
        4. Getting and setting data with DynamoDB
          1. Searching the database
        5. Sending mail via SES
      6. Authenticating with Facebook Connect
      7. Summary
    17. 9. Testing your Application
      1. Why testing is important
        1. Unit tests
        2. Functional tests
        3. Integration tests
      2. Native Node testing and debugging tools
        1. Writing to the console
          1. Formatting console output
            1. The util.format(format, [arg, arg…]) method
            2. The util.inspect(object, [options]) method
        2. The Node debugger
        3. The assert module
        4. Sandboxing
          1. Distinguishing between local scope and execution context
          2. Using compiled contexts
        5. Errors and exceptions
        6. The domain module
      3. Headless website testing with ZombieJS and Mocha
        1. Mocha
        2. Headless web testing
      4. Using Grunt, Mocha, and PhantomJS to test and deploy projects
        1. Working with Grunt
      5. Summary
    18. A. Organizing Your Work
      1. Loading and using modules
        1. Understanding the module object
        2. Resolving module paths
      2. Using npm
        1. Initializing a package file
        2. Using scripts
        3. Declaring dependencies
        4. Publishing packages
        5. Globally installing packages and binaries
        6. Sharing repositories
    19. B. Introducing the Path Framework
      1. Managing state
      2. Bridging the client/server divide
      3. Sending and receiving
      4. Achieving a modular architecture
    20. C. Creating your own C++ Add-ons
      1. Hello World
      2. Creating a calculator
      3. Implementing callbacks
      4. Closing thoughts
      5. Links and resources
    21. Index