Welcome to the world of developing web software using Node (also known as Node.js). Node is a newly-developed 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 developing web server components in Ruby and other languages. The 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 Twisted framework (Python).
This book, Node Web Development, focuses on building web applications using Node. We will be taking a tour through the important concepts required to speed up with Node. To do so we'll be writing real applications, dissecting them to scrutinize how they work, 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 develop several applications, ponder the effects of long-running calculations on event loop responsiveness, look at a couple of ways to distribute heavy workloads to other servers, work with the Express framework, and more.
Chapter 1, What is Node?, introduces you to the Node platform. We cover its uses, the technological architectural choices in Node, its history, and the history of server-side JavaScript, and why JavaScript should remain trapped in browsers.
Chapter 2, Setting up Node, goes over setting up a Node developer environment, including several scenarios of compiling and installing from source code. We briefly touch on Node deployment to production servers.
Chapter 3, Node Modules, explains that modules are the unit of modularity in developing Node applications. We take a dive into understanding and developing Node modules. We then take a close look at npm, the Node Package Manager, and several scenarios using npm to manage installed packages, or to develop npm packages and distribute them for others.
Chapter 4, Variations on a Simple Application, explains that with the fundamentals in hand we begin exploring application development in Node. Specifically we develop a simple application using Node itself, the Connect middleware framework, and the Express application framework. While the application is simple, it gives us a chance to explore the Node event loop, accommodating long running calculations, asynchronous and synchronous algorithms, and pushing heavy calculations to a backend server.
Chapter 5, A Simple Web Server, EventEmitters, and HTTP Clients, explains that in Node the HTTP client and server objects are front and center. We take a close look at both ends of the HTTP conversation by developing both HTTP client and server applications.
Chapter 6, Data Storage and Retrieval, explains that most applications need some sort of long-term reliable data storage. We look at implementing an application with both SQL and MongoDB database engines. Along the way we cover user authentication and presenting a better error page, using the Express framework.
Today, we normally install Node from source, and it works best on Unix- or POSIX-like systems. The requirements to begin using Node are modest, and your most important tool is the one between your ears.
Installing from source requires a Unix-/POSIX-like system (Linux, Mac, FreeBSD, OpenSolaris, and so on), modern C/C++ compiler, the OpenSSL libraries, and Python version 2.4 or later.
Node programs can be edited with any text editor, but one that can handle JavaScript, HTML, CSS, and so on will be useful.
While the book is about developing web applications, it does not require you to have a web server. Node provides its own web server stack.
This book was 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 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 a new territory, and to write in JavaScript without accessing the DOM. (There's no browser, hence no DOM, unless you install JSDom.)
While the chapters build on each other, how you read this book is up to you.
We assume you already know how to write software, and have an understanding of modern programming languages such as JavaScript.
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: "The http
object encapsulates the HTTP protocol and its http.createServer
method creates a whole web server, listening on the port specified in the .listen
method."
A block of code is set as follows:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
var util = require('util');
var A = "a different value A";
var B = "a different value B";
var m1 = require('./module1');
util.log('A='+A+' B='+B+' values='+util.inspect(m1.values()));
Any command-line input or output is written as follows:
$ sudo /usr/sbin/update-rc.d node defaults
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: "A real security system would have fields for at least a username and password. Instead we'll skip this and just ask the user to click the Login button."
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 <[email protected]>
, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>
.
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.
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.
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.
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/support, 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 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.