Table of Contents Node Web Development Second Edition Credits About the Author Acknowledgement About the Reviewers www.PacktPub.com Support files, eBooks, discount offers and more Why Subscribe? Free Access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy Questions 1. About Node The capabilities of Node Server-side JavaScript Why should you use Node? Threaded versus asynchronous event-driven architecture Performance and utilization Is Node a cancerous scalability disaster? Server utilization, the bottom line, and green web hosting What's in a name – Node, Node.js, or Node.JS? Summary 2. Setting up Node System requirements Installing Node using package managers Installing on Mac OS X with MacPorts Installing on Mac OS X with Homebrew Installing on Linux from package management systems Installing the Node distribution from nodejs.org Installing Node on Windows using Chocolatey Gallery Installing the StrongLoop Node distribution Installing from source on POSIX-like systems Installing prerequisites Installing developer tools on Mac OS X Installing from source for all POSIX-like systems Maintaining multiple Node installs simultaneously Run a few commands; testing the commands Node's command-line tools Running a simple script with Node Launching a server with Node npm – the Node package manager Starting Node servers at system startup Summary 3. Node Modules Defining a module Node modules Node's algorithm for resolving require(module) Module identifiers and path names Local modules within your application Bundling external dependencies with your application System-wide modules in NODE_PATH and elsewhere Complex modules – modules as directories Node package manager The npm package format Finding npm packages Using the npm commands Getting help with npm Viewing package information Installing an npm package Installing native code modules on Windows Installing packages local to a module Eliminating duplicate modules installed beneath node_modules Listing the currently installed packages Package scripts Editing and exploring installed package content Updating outdated packages you've installed Uninstalling an installed npm package Developing and publishing npm packages npm configuration settings Package version strings and ranges CommonJS modules Demonstrating module encapsulation Summary 4. HTTP Servers and Clients – A Web Application's First Steps Sending and receiving events with EventEmitters EventEmitter theory HTTP server applications HTTP Sniffer – listening to the HTTP conversation Web application frameworks Getting started with Express Walking through the default Express application Calculating the Fibonacci sequence with Express Computationally intensive code and the event loop Algorithmic refactoring Making HTTP Client requests Calling a REST backend service from an Express application Implementing a simple REST server with Express Refactoring the Fibonacci application for REST Some RESTful Node modules Summary 5. Implementing a Simple Express Application Express and the MVC paradigm Creating the Notes application code The Notes model The Notes home page Adding a new note (create) Viewing notes (read) Editing an existing note (update) Deleting notes (destroy) Changing the look of an Express application Scaling up and running multiple instances Summary 6. Data Storage and Retrieval Asynchronizing the Notes application Injecting the model configuration into routers The notes router Storing notes in files Configuring app.js Storing notes with the LevelUP data store Installing LevelUP LevelUP model code for Notes Configuring app.js for LevelUP Storing notes in SQL – SQLite3 Setting up a schema with SQLite3 Model code Configuring app.js Storing notes the ORM way with the Sequelize module Schema setup and model code Configuring app.js Storing notes in MongoDB with Mongoose Implementing the Notes model in Mongoose Configuring app.js Summary 7. Multiuser Authorization, Deployment, Scaling, and Hosting User authentication Changes in app.js The Sequelize-based users model Routing module for the login, logout, and account pages Initializing the user table Running the Notes application Deploying Notes on Debian Scaling to use all cores on multi-core servers Deploying Notes on cloud hosting (AppFog) Summary 8. Dynamic Interaction between the Client and Server Application Adding real-time web features to Notes Introducing Socket.IO Initializing Socket.IO with Express Setting up the client code Events between the Notes server and client code Modifying the Notes model to send events Sending the events from the Notes server Browser-side event handlers Running the Notes application with Socket.IO Listening to the heartbeat and cleaning up when it stops Sending messages between users Socket.IO events for sending messages between users Data model to store messages Setting up client-side code for sending messages Dispatching messages between the client-and server-side Displaying messages to the user Running Notes and sending messages Summary 9. Unit Testing Testing asynchronous code Assert – the simplest testing methodology Testing a model Executing the tests Testing router functions Diagnosing a failing test case Making it easy to run the tests Summary Index Node Web Development Second Edition Node Web Development Second Edition Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2011 Second Edition: July 2013 Production Reference: 1120713 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-330-5 www.packtpub.com Cover Image by Suresh Mogre (<[email protected]>) Credits Author David Herron Reviewers Ollie Bennett Nathan Zondlo Acquisition Editors Joanne Fitzpatrick Sarah Cullington Lead Technical Editor Mayur Hule Sruthi Kutty Technical Editor Jeeten Handu Ankita Meshram Project Coordinators Wendell Palmer Abhishek Kori Proofreader Maria Gould
Description: