Node Cookbook

Book description

A hands on approach to learning Node to equip and guide you in the art of asynchronous server side JavaScript with this book and ebook.

  • Packed with practical recipes taking you from the basics to extending Node with your own modules.
  • Create your own web server to see Node’s features in action.
  • Work with JSON, XML, web sockets, and make the most of asynchronous programming.

In Detail

The principles of asynchronous event-driven programming are perfect for today's web, where efficient real-time applications and scalability are at the forefront. Server-side JavaScript has been here since the 90's but Node got it right. With a thriving community and interest from Internet giants, it could be the PHP of tomorrow.

"Node Cookbook" shows you how to transfer your JavaScript skills to server side programming. With simple examples and supporting code, "Node Cookbook" talks you through various server side scenarios often saving you time, effort, and trouble by demonstrating best practices and showing you how to avoid security faux pas.

Beginning with making your own web server, the practical recipes in this cookbook are designed to smoothly progress you to making full web applications, command line applications, and Node modules. Node Cookbook takes you through interfacing with various database backends such as MySQL, MongoDB and Redis, working with web sockets, and interfacing with network protocols, such as SMTP. Additionally, there are recipes on correctly performing heavy computations, security implementations, writing, your own Node modules and different ways to take your apps live

Table of contents

  1. Node Cookbook
    1. Table of Contents
    2. Node Cookbook
    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
      7. Downloading the example code
      8. Errata
      9. Piracy
      10. 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
          3. 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.bodyParser
        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 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...
          1. Enabling resumes from broken downloads
        4. See also
    10. 3. Working with Data Serialization
      1. Introduction
      2. Converting an object to JSON and back again
        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 again
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Objects containing arrays and functions
          2. Generating XML attributes
          3. Text values alongside attribute declarations
        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 client to server
        5. See also
      5. Working with real data: fetching trending tweets
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. Twitter API and the User-Agent header
        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 CSV as a 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
        5. See also
      5. Storing and retrieving data with Mongoskin
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Collection binding
        5. See also
      6. 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
      7. 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
      8. Accessing CouchDB changes stream with Cradle
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. 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 node Redis module
          2. Overcoming network latency by pipelining commands
        5. See also
      10. 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. Transcending AJAX: Using WebSockets
      1. Introduction
      2. Creating a WebSocket server
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Supporting older Firefox browsers
          2. Creating a node-based WebSocket client
        5. See also
      3. Seamless fallbacking with socket.io
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Custom events
          2. Namespaces
        5. See also
      4. Callbacks over socket.io transport
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Shared functions with Nowjs
        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
    13. 6. 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. Looking into routes/index.js
        5. See also
      3. Defining and applying 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. 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 partials
          5. Express partials
          6. Jade includes
          7. layout.jade
        5. See also
      6. CSS engines 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 the LESS engine
        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. App mounting
            1. Data Flow
          2. Route handling
          3. Views
          4. Mixins
          5. Helpers
          6. Styles
          7. User flow
        4. There's more...
          1. Benchmarking
          2. Using logger
        5. See also
    14. 7. 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. Cryptographic password hashing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Uniquifying 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...
          1. Cross-site scripting (XSS) circumvention
        4. There's more...
          1. Auto-securing the POST forms with the CSRF elements
          2. Eliminating cross-site scripting (XSS) vulnerabilities
        5. See also
    15. 8. Integrating Network Paradigms
      1. Introduction
      2. Sending email
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using sendmail as an alternative transport
          2. HTML emails
          3. Sending attachments
        5. See also
      3. Sending SMS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the processed event listener
          2. 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 emails 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
        5. See also
    16. 9. Writing Your Own Node Modules
      1. Introduction
      2. Creating a test-driven module API
        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 from functional to prototypical
        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 for multiple instances
        5. See also
      5. Extending the module's API
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Making the STDIN stream example
          2. Making 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 version
        5. See also
    17. 10. Taking It Live
      1. Introduction
      2. Deploying 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
    18. Index

Product information

  • Title: Node Cookbook
  • Author(s): David Mark Clements
  • Release date: July 2012
  • Publisher(s): Packt Publishing
  • ISBN: 9781849517188