Book Image

Node Web Development - Second Edition

By : David Herron
Book Image

Node Web Development - Second Edition

By: David Herron

Overview of this book

Table of Contents (17 chapters)
Node Web Development Second Edition
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Welcome to the world of developing web software with Node (also known as Node.js). Node is an up-and-coming software platform that liberates JavaScript from the web browser, enabling it to be used as a general software development platform in server-side applications. It runs atop the ultra-fast JavaScript engine from the Chrome browser, V8, and adds in a fast and robust library of asynchronous network I/O modules. The primary focus of Node is on building high performance, highly scalable, server and client applications for the "real-time Web".

The platform was developed by Ryan Dahl in 2009 after a couple of years of experimenting with web server component development in Ruby and other languages. Today, Node's development is sponsored by Joyent, and is led by a core team of several members from several companies. Dahl's exploration led him to the architectural choice of using asynchronous event-driven systems rather than the traditional thread-based concurrency model.

This model was chosen because it's simpler (threaded systems are notoriously difficult to develop), has lower overhead over maintaining a thread per connection, and for speed. The goal of Node is to provide an "easy way to build scalable network servers". The design is similar to, and influenced by, other systems, such as Event Machine (Ruby) and the Twisted framework (Python).

JavaScript was chosen because the anonymous functions and other language elements are an excellent method for implementing asynchronous computation, such as event handlers. Handler functions are often able to be written in-line with the invoking code, just by providing an anonymous function. The runtime library of built-in modules are ingeniously designed to make good use of asynchronous coding.

The result is a platform allowing developers to not only succinctly write code of great power, but to have a lot of fun while doing so.

JavaScript on both the server and client (browser) lets us implement a vision dating back to the days when Java's proponents first showed us dynamic stuff running inside a web page. Java never became the language for this, and JavaScript is quickly advancing to take on this vision in a real big way. By having JavaScript on both ends of the wire, we can use common code, common data structures, and in general have an easier time of things.

This book, Node Web Development, Second Edition, focuses on building web applications using Node. We assume you have some knowledge of JavaScript, and maybe even have server-side development experience. We will be taking a tour through the important concepts to understand programming with Node.

To do so, we'll be developing several applications, including one that's somewhat useful, and this will be developed in stages over several chapters. That application will, in the end, support real-time updates to multiple users simultaneously, as well as real-time messaging between users.

We'll be dissecting the code to scrutinize how it works, and discussing how to apply the ideas to your own programs. We'll install Node and npm, and learn how to install or develop npm packages and Node modules. We'll look at how to distribute heavy workloads to backend servers, implement simple REST services, and even how to do real-time communication between the server and client.

What this book covers

Chapter 1, About Node, introduces you to the Node platform. It covers its uses, the technological architectural choices in Node, its history, the history of server-side JavaScript, and why JavaScript should be liberated from the browser.

Chapter 2, Setting up Node, goes over setting up a Node developer environment, including compiling and installing from source code. We briefly touch on Node deployment to production servers.

Chapter 3, Node Modules, explores the module as the unit of modularity in developing Node applications. We dive deep into understanding and developing Node modules, at npm, the Node Package Manager, and several scenarios using npm to manage installed packages, or developing npm packages for distribution to others.

Chapter 4, HTTP Servers and Clients – A Web Application's First Steps, starts exploring web development with Node, with the fundamentals of Node now in hand. We develop several small web-server and web-client applications in Node. We use the Fibonacci algorithm to explore the effects of heavy-weight long-running computations on a Node web application, and several mitigation techniques, including developing a simple REST-based backend server to compute Fibonacci numbers.

Chapter 5, Implementing a Simple Express Application, begins several chapters of developing a somewhat useful application with which we'll be exploring the Node platform. In this chapter we build the core skeleton of the application.

Chapter 6, Data Storage and Retrieval, helps add data storage capabilities to the application for data persistence, application robustness, and sharing data between application instances. The data storage engines we look at include the file system, LevelUP, SQLite3, Sequelize, MySQL, and MongoDB (using Mongoose).

Chapter 7, Multiuser Authorization, Deployment, Scaling, and Hosting, shows how to add user authentication capabilities to the application, allowing it to be used by both logged-in and anonymous users. We then look at several ways of deploying the application on real servers, including a cloud hosting platform.

Chapter 8, Dynamic Interaction between the Client and Server Application, explores the real-time web by using the Socket.IO library to enable real-time communication between the server and client. We experience firsthand the power of having JavaScript on both server and client.

Chapter 9, Unit Testing, shows how to implement unit testing and test first development methodologies with a Node application. We use this to find a few bugs in the code written for the previous chapters, and then walk through fixing those bugs.

What you need for this book

The basic requirement is to have Node installed, and to have a programmer-oriented text editor. We show you how to install Node either from a prebuilt binary package, or from source. The most important tool is the one between your ears. The examples have been tested against Node v0.10.x.

Some of the chapters require database engines such as MySQL and MongoDB.

Some of the modules that we'll install require compiling from source code. That requires a C/C++ compiler toolchain plus Python 2.7 or later (but not Python 3.0 or later).

While this book is about developing web applications, it does not require you to have a web server. Node provides its own web server stack.

Who this book is for

This book is written for any software engineer who wants the adventure that comes with a new software platform embodying a new programming paradigm.

Server-side engineers may find the concepts behind Node refreshing, giving you a different perspective on web application development. JavaScript is a powerful language and Node's asynchronous nature plays to JavaScript's strengths.

Developers experienced with JavaScript in the browser may find it fun to bring that knowledge to new territory.

We assume that you already know how to write software, and have an understanding of modern programming languages such as JavaScript.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "With the node_modules directory situated where it is, any module within drawapp can access express with the code."

A block of code is set as follows:

#!/usr/bin/env node
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337);

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

exports.view = function(req, res, next) {
    var user = req.user ? req.user : undefined;
    if (req.query.key) {

        readNote(req.query.key, user, res,function(err, data)

Any command-line input or output is written as follows:

$ sudo forever list
info: Forever processes running
data: uid command script forever pid logfile uptime       

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Once you're registered, go to the dashboard and click on the Create App button".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.