![]()
Table of Contents
Node Web Development
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. What is Node?
What can you do with Node?
Server-side JavaScript
Why should you use Node?
Architecture: Threads versus asynchronous event-driven
Performance and utilization
Server utilization, the bottom line, and green web hosting
Spelling: Node, Node.js, or Node.JS?
Summary
2. Setting up Node
System requirements
Installation on POSIX-like systems (Linux, Solaris, Mac, and so on)
Installing prerequisites
Installing developer tools on Mac OS X
Installing in your home directory
What's the rationale for a home directory installation?
Installing in a system-wide directory
Installing on Mac OS X with MacPorts
Installing on Mac OS X with homebrew
Installing on Linux from package management systems
Maintaining multiple Node installs simultaneously
Run a few commands; test your installation
Node's command-line tools
Running a simple script with Node
Launching a server with Node
Installing npmâthe Node package manager
Starting Node servers at system startup
Using all CPU cores on multi-core systems
Summary
3. Node Modules
What's a module?
Node modules
How does Node resolve require('module')?
Module identifiers and path names
Local modules within your application
Bundling external dependencies with your application
System-wide modules in the require.paths directories
Complex modulesâmodules as directories
Node package Manager (npm)
npm package format
Finding npm packages
Using the npm commands
Getting help with npm
Viewing package information
Installing an npm package
Using installed packages
What packages are currently installed?
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. Variations on a Simple Application
Creating a Math Wizard
To use a web framework, or not
Implementing the Math Wizard with Node (no frameworks)
Routing requests in Node
Handling URL query parameters
Multiplying numbers
Calculating the other mathematical functions
Extending the Math Wizard
Long running calculations (fibonacci numbers)
What "complete web server" features are missing?
Using Connect to implement the Math Wizard
Installing Connect and other setup
Connecting with Connect
Using Express to implement the Math Wizard
Implementing the Express Math Wizard
Handling errors
Parameterized URLs and data services
Parametrized URLs in Express
The mathematics server (and client)
Refactoring Math Wizard to use math server
Summary
5. A Simple Web Server, EventEmitters, and HTTP Clients
Sending and receiving events with EventEmitters
EventEmitter theory
HTTP Snifferâlistening to the HTTP conversation
Implementing a basic web server
The Basic Server implementation
Basic Server core (basicserver.js)
The Favicon handler (faviconHandler.js)
The static file handler (staticHandler.js)
A configuration for Basic Server (server.js)
Virtual host configuration with Basic Server
A shorturl module for Basic Server
MIME types and the MIME npm package
Cookie handling
Virtual hosts and request routing
Making HTTP Client requests
Summary
6. Data Storage and Retrieval
Data storage engines for Node
SQLite3âLightweight in-process SQL engine
Installation
Implementing the Notes application with SQLite3
Database abstraction moduleânotesdb-sqlite3.js
Initializing the databaseâsetup.js
Display notes on the consoleâshow.js
Putting together the Notes web applicationâapp.js
Notes application templates
Running the SQLite3 Notes application
Handling and debugging errors
Using other SQL databases with Node
MongooseâNode interface to MongoDB
Installing Mongoose
Implementing the Notes application with Mongoose
Database abstraction moduleânotesdb-mongoose.js
Initializing the databaseâsetup.js
Display notes on the consoleâshow.js
Putting it together in an applicationâapp.js
Other MongoDB database support
A quick look at authenticating your users
Summary
Index
![]()
Copyright © 2011 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
Production Reference: 1020811
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-849515-14-6
www.packtpub.com
![]()
Author
David Herron
Reviewers
Blagovest Dachev
Matt Ranney
Acquisition Editor
Sarah Cullington
Development Editor
Pallavi Iyengar
Technical Editor
Joyslita D'Souza
Project Coordinator
Joel Goveya
Proofreader
Aaron Nash
Indexers
Hemangini Bari
Tejal Daruwale
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
![]()
David Herron has worked in the software industry, holding both developer and quality engineering roles, in Silicon Valley for over 20 ye...