You are previewing Node Cookbook Second Edition.
O'Reilly logo
Node Cookbook Second Edition

Book Description

Transferring your JavaScript skills to server-side programming is simplified with this comprehensive cookbook. Each chapter focuses on a different aspect of Node, featuring recipes supported with lots of illustrations, tips, and hints.

In Detail

Node Cookbook Second Edition shows you how to transfer your JavaScript skills to server-side programming. It will talk you through the various server-side scenarios, often saving you time and effort by demonstrating best practices and showing you how to avoid security faux pas.

Beginning with making your own web server, the recipes are designed to allow smooth progression through Node in order to help you design full web applications, command-line applications, and Node modules.

There's also extensive coverage on using popular frameworks such as Express 4 and Socket.IO, writing your own Node modules and different ways to take your apps live.

What You Will Learn

  • Create different kinds of servers
  • Manipulate all kinds of data
  • Interface with various databases
  • Use streams for efficient, powerful data shuffling
  • Create real-time apps
  • Create command-line apps
  • Write and publish your own modules
  • Work with the Express framework
  • Get to grips with the module ecosystem
  • Explore with external remote APIs
  • Learn various security, encryption, and authentication techniques
  • 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 files e-mailed directly to you.

    Table of Contents

    1. Node Cookbook Second Edition
      1. Table of Contents
      2. Node Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      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. Making a Web Server
        1. Introduction
        2. Setting up a router
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Simple multilevel routing
            2. Parsing the querystring module
            3. The routing modules
          5. See also
        3. Serving static files
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. The favicon gotcha
          5. See also
        4. Caching content in memory for immediate delivery
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Reflecting content changes
          5. See also
        5. Optimizing performance with streaming
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Protecting against process memory overruns
          5. See also
        6. Securing against filesystem hacking exploits
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Whitelisting
            2. Node static
          5. See also
      9. 2. Exploring the HTTP Object
        1. Introduction
        2. Processing POST data
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Accessing POST data with connect and body-parser
          5. See also
        3. Handling file uploads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using formidable to accept all POST data
            2. Preserving filenames with formidable
            3. Uploading files via PUT
          5. See also
        4. Using Node as an HTTP client
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Sending POST requests
            2. Multipart file upload as a client
          5. See also
        5. Implementing download throttling
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Enabling a resume request from broken downloads
          5. See also
      10. 3. Working with Data Serialization
        1. Introduction
        2. Converting an object to JSON and back
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Constructing JSONP responses
            2. Security and JSONP
          5. See also
        3. Converting an object to XML and back
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Generating XML attributes and text nodes
            2. Partial application
          5. See also
        4. Browser-server transmission via AJAX
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Sending serialized data from the client to the server
          5. See also
        5. Working with real data – fetching trending tweets
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Cross referencing Google Hot Trends with Twitter tweets
          5. See also
      11. 4. Interfacing with Databases
        1. Introduction
        2. Writing to a CSV file
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Customizing the CSV elements
            2. Reading a CSV file
            3. Manipulating the CSV data stream
          5. See also
        3. Connecting and sending SQL to a MySQL server
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Using and cleaning user input
            2. Receiving results from the MySQL server
          5. See also
        4. Storing and retrieving data with MongoDB
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Indexing and aggregation
            2. Updating modifiers, sort, and limit
            3. MongoDB without MongoDB
          5. See also
        5. Storing data to CouchDB with Cradle
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Scaling CouchDB with BigCouch
          5. See also
        6. Retrieving data from CouchDB with Cradle
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Creating an admin user
            2. Locking all modifying operations to an admin user
            3. Exposing the CouchDB HTTP interface to remote connections
          5. See also
        7. Accessing the CouchDB changes stream with Cradle
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Storing and retrieving data with Redis
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Speeding up the Node Redis module
            2. Overcoming network latency by pipelining commands
          5. See also
        9. Implementing PubSub with Redis
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Redis authentication
            2. Securing Redis from external connections
          5. See also
      12. 5. Employing Streams
        1. Introduction
        2. Consuming streams
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Using read's size argument
            2. Consuming via the data event
          5. See also
        3. Playing with pipes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Chaining and filtering streams
            2. Preparing for greater complexity
          5. See also
        4. Making stream interfaces
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Making reusable streams
            2. Transform streams
          5. See also
        5. Streaming across Node processes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Processing stream chunk buffers efficiently
            2. Streaming over TCP
          5. See also
      13. 6. Going Real Time
        1. Introduction
        2. Creating a WebSocket server
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Creating a Node-based WebSocket client
            2. WebSocket streams
          5. See also
        3. Cross-browser real-time logic with Socket.IO
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Custom events
          5. See also
        4. Remote Procedure Calls with Socket.IO
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Remote Procedure Calls with SockJS
          5. See also
        5. Creating a real-time widget
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Preparing for scalability
            2. WebSockets as a development tool
          5. See also
      14. 7. Accelerating Development with Express
        1. Introduction
        2. Generating Express scaffolding
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Picking apart app.js
            2. The initialization process
            3. Looking into routes/index.js
          5. See also
        3. Managing server tier environments
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Setting other environments
            2. Changing NODE_ENV permanently
          5. See also
        4. Implementing dynamic routing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Route validation
            2. Optional routes
            3. Asterisks wildcards
          5. See also
        5. Templating in Express
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using other template engines
            2. EJS templates
            3. Literal JavaScript in Jade
            4. Jade includes
            5. Using layout.jade
          5. See also
        6. CSS preprocessors with Express
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Nested mixins and rest parameters
            2. Playing with colors
            3. Using LESS
          5. See also
        7. Initializing and using a session
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Custom middleware for site-wide session management
            2. Flash messages
          5. See also
        8. Making an Express web app
          1. Getting ready
          2. How to do it...
            1. Creating a database bridge
            2. Configuring app.js files
            3. Modifying the profiler app
            4. Modifying the mounted login app
          3. How it works...
            1. Understanding app mounting
              1. Data flow
            2. Route handling
            3. Views
            4. Mixins
            5. Locals
            6. Styles
            7. User flow
          4. There's more...
            1. Benchmarking
          5. See also
      15. 8. Implementing Security, Encryption, and Authentication
        1. Introduction
        2. Implementing Basic Authentication
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Basic Authentication with Express
          5. See also
        3. Hashing passwords
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Making unique hashes with HMAC
            2. Hardened hashing with PBKDF2
          5. See also
        4. Implementing Digest Authentication
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Logging out of authenticated areas
          5. See also
        5. Setting up an HTTPS web server
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. HTTPS in Express
            2. Securing Basic Authentication with SSL/TLS
          5. See also
        6. Preventing cross-site request forgery
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Auto-securing the POST forms with the CSRF elements
            2. Eliminating cross-site scripting (XSS) vulnerabilities
          5. See also
      16. 9. Integrating Network Paradigms
        1. Introduction
        2. Sending an e-mail
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using sendmail as an alternative transport
            2. Creating HTML e-mails
            3. Sending attachments
          5. See also
        3. Sending an SMS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Making an automated phone call
          5. See also
        4. Communicating with TCP
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Port forwarding
            2. Using pcap to watch TCP traffic
          5. See also
        5. Creating an SMTP server
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Receiving e-mails from external SMTP servers
          5. See also
        6. Implementing a virtual hosting paradigm
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Virtual hosting Express apps
            2. Server Name Indication (SNI)
          5. See also
      17. 10. Writing Your Own Node Modules
        1. Introduction
        2. Creating a test-driven module specification
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Unit tests with should.js
          5. See also
        3. Writing a functional module mock-up
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Writing a module use case example
          5. See also
        4. Refactoring with prototypical inheritance
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Adding the stat function to the initialized mp3dat object
            2. Allowing multiple instances
          5. See also
        5. Extending a module's API
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Creating the STDIN stream example
            2. Creating the PUT upload stream example
            3. Merging stat and statStream
            4. Integrating the EventEmitter
          5. See also
        6. Deploying a module to npm
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. npm link
            2. .npmignore and npm versions
          5. See also
      18. 11. Taking It Live
        1. Introduction
        2. Deploying an app to a server environment
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using screen instead of nohup
            2. Using authbind for privileged ports
            3. Hosting multiple processes from port 80
          5. See also
        3. Automatic crash recovery
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Detecting a respawn limit violation
            2. Staying up with forever
          5. See also
        4. Continuous deployment
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Building module dependencies on update
            2. Writing a Node Git hook for integrated testing
          5. See also
        5. Hosting with a Platform as a Service provider
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Assigning custom domains to Nodejitsu apps
            2. Provisioning a database with jitsu
          5. See also
      19. Index