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

Node Web Development

Book Description

A practical introduction to Node, the exciting new server-side JavaScript web development stack

  • Go from nothing to a database-backed web application in just over 100 pages
  • Get started quickly with Node and discover that JavaScript is not just for browsers anymore
  • An introduction to server-side JavaScript with Node, the Connect and Express frameworks, and using SQL or MongoDB database back-end

In Detail

Node is an exciting new technology stack that brings JavaScript to the server-side of web applications for the first time. Node means that JavaScript is no longer just for browsers. It's for web application development, it's for developing any internet protocol, it's for the real-time web, it's for command line scripts, and much more.

Node Web Development gives you an excellent starting point straight into the heart of developing server side web applications with node. You will learn, through practical examples, how to use the HTTP Server and Client objects, the Connect and Express application frameworks, the algorithms for asynchronous execution, and use both SQL and MongoDB databases.

This book is the ideal companion for getting started with Node. Starting with practical advice on installing Node for both development and application deployment, you will learn how to develop both HTTP Server and Client applications. Many different ways of working with Node are shown, including using database storage engines in applications and developing websites both with and without the Connect/Express web application framework. You will also get an introduction to Node’s CommonJS module system allowing you to implement an important subset of object-oriented design.

Table of Contents

  1. Node Web Development
    1. Node Web Development
    2. Credits
    3. About the Author
    4. Acknowledgement
    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. What is Node?
      1. What can you do with Node?
        1. Server-side JavaScript
      2. Why should you use Node?
        1. Architecture: Threads versus asynchronous event-driven
        2. Performance and utilization
        3. Server utilization, the bottom line, and green web hosting
      3. Spelling: Node, Node.js, or Node.JS?
      4. Summary
    9. 2. Setting up Node
      1. System requirements
      2. Installation on POSIX-like systems (Linux, Solaris, Mac, and so on)
        1. Installing prerequisites
      3. Installing developer tools on Mac OS X
        1. Installing in your home directory
          1. What's the rationale for a home directory installation?
        2. Installing in a system-wide directory
        3. Installing on Mac OS X with MacPorts
        4. Installing on Mac OS X with homebrew
        5. Installing on Linux from package management systems
        6. Maintaining multiple Node installs simultaneously
      4. Run a few commands; test your installation
        1. Node's command-line tools
        2. Running a simple script with Node
        3. Launching a server with Node
      5. Installing npm—the Node package manager
      6. Starting Node servers at system startup
        1. Using all CPU cores on multi-core systems
      7. Summary
    10. 3. Node Modules
      1. What's a module?
        1. Node modules
        2. How does Node resolve require('module')?
          1. Module identifiers and path names
          2. Local modules within your application
          3. Bundling external dependencies with your application
          4. System-wide modules in the require.paths directories
          5. Complex modules—modules as directories
      2. Node package Manager (npm)
        1. npm package format
        2. Finding npm packages
        3. Using the npm commands
          1. Getting help with npm
          2. Viewing package information
          3. Installing an npm package
          4. Using installed packages
          5. What packages are currently installed?
          6. Package scripts
          7. Editing and exploring installed package content
          8. Updating outdated packages you've installed
          9. Uninstalling an installed npm package
          10. Developing and publishing npm packages
          11. npm configuration settings
        4. Package version strings and ranges
        5. CommonJS modules
          1. Demonstrating module encapsulation
      3. Summary
    11. 4. Variations on a Simple Application
      1. Creating a Math Wizard
        1. To use a web framework, or not
      2. Implementing the Math Wizard with Node (no frameworks)
        1. Routing requests in Node
        2. Handling URL query parameters
        3. Multiplying numbers
        4. Calculating the other mathematical functions
        5. Extending the Math Wizard
        6. Long running calculations (fibonacci numbers)
        7. What "complete web server" features are missing?
        8. Using Connect to implement the Math Wizard
        9. Installing Connect and other setup
        10. Connecting with Connect
      3. Using Express to implement the Math Wizard
        1. Implementing the Express Math Wizard
        2. Handling errors
        3. Parameterized URLs and data services
          1. Parametrized URLs in Express
          2. The mathematics server (and client)
          3. Refactoring Math Wizard to use math server
      4. Summary
    12. 5. A Simple Web Server, EventEmitters, and HTTP Clients
      1. Sending and receiving events with EventEmitters
        1. EventEmitter theory
      2. HTTP Sniffer—listening to the HTTP conversation
      3. Implementing a basic web server
        1. The Basic Server implementation
          1. Basic Server core (basicserver.js)
          2. The Favicon handler (faviconHandler.js)
          3. The static file handler (staticHandler.js)
          4. A configuration for Basic Server (server.js)
          5. Virtual host configuration with Basic Server
          6. A shorturl module for Basic Server
      4. MIME types and the MIME npm package
      5. Cookie handling
      6. Virtual hosts and request routing
      7. Making HTTP Client requests
      8. Summary
    13. 6. Data Storage and Retrieval
      1. Data storage engines for Node
      2. SQLite3—Lightweight in-process SQL engine
        1. Installation
        2. Implementing the Notes application with SQLite3
          1. Database abstraction module—notesdb-sqlite3.js
          2. Initializing the database—setup.js
          3. Display notes on the console—show.js
          4. Putting together the Notes web application—app.js
          5. Notes application templates
          6. Running the SQLite3 Notes application
          7. Handling and debugging errors
        3. Using other SQL databases with Node
      3. Mongoose—Node interface to MongoDB
        1. Installing Mongoose
        2. Implementing the Notes application with Mongoose
          1. Database abstraction module—notesdb-mongoose.js
          2. Initializing the database—setup.js
          3. Display notes on the console—show.js
          4. Putting it together in an application—app.js
        3. Other MongoDB database support
      4. A quick look at authenticating your users
      5. Summary