Getting Started with Node.js
Node.js is a website/application framework designed with high scalability in mind. Get an introduction to the Node.js environment.
Save 35% off the list price* of the related book or multi-format eBook (EPUB + MOBI + PDF) with discount code ARTICLE.
* See informit.com/terms
This chapter introduces you to the Node.js environment. Node.js is a website/application framework designed with high scalability in mind. It was designed to take advantage of the existing JavaScript technology in the browser and flow those same concepts all the way down through the webserver into the backend services. Node.js is a great technology that is easy to implement and yet extremely scalable.
Node.js is a modular platform, meaning that much of the functionality is provided by external modules rather than being built in to the platform. The Node.js culture is active in creating and publishing modules for almost every imaginable need. Therefore, much of this chapter focuses on understanding and using the Node.js tools to build, publish, and use your own Node.js modules in applications.
Understanding Node.js
Node.js was developed in 2009 by Ryan Dahl as an answer to the frustration caused by concurrency issues, especially when dealing with web services. Google had just come out with the V8 JavaScript engine for the Chrome web browser, which was highly optimized for web traffic. Dahl created Node.js on top of V8 as a server-side environment that matched the client-side environment in the browser.
The result is an extremely scalable server-side environment that allows developers to more easily bridge the gap between client and server. The fact that Node.js is written in JavaScript allows developers to easily navigate back and forth between client and server code and even reuse code between the two environments.
Node.js has a great ecosystem with new extensions being written all the time. The Node.js environment is clean and easy to install, configure, and deploy. Literally in only an hour or two you can have a Node.js webserver up and running.
Who Uses Node.js?
Node.js quickly gained popularity among a wide variety of companies. These companies use Node.js first and foremost for scalability but also for ease of maintenance and faster development. The following are just a few of the companies using the Node.js technology:
Yahoo!
LinkedIn
eBay
New York Times
Dow Jones
Microsoft
What Is Node.js Used For?
Node.js can be used for a wide variety of purposes. Because it is based on V8 and has highly optimized code to handle HTTP traffic, the most common use is as a webserver. However, Node.js can also be used for a variety of other web services such as:
Web services APIs such as REST
Real-time multiplayer games
Backend web services such as cross-domain, server-side requests
Web-based applications
Multiclient communication such as IM
What Does Node.js Come With?
Node.js comes with many built-in modules available right out of the box. This book covers many but not all of these modules:
Assertion testing: Allows you to test functionality within your code.
Buffer: Enables interaction with TCP streams and file system operations. (See Chapter 5, “Handling Data I/O in Node.js.”)
C/C++ add-ons: Allows for C or C++ code to be used just like any other Node.js module.
Child processes: Allows you to create child processes. (See Chapter 9, “Scaling Applications Using Multiple Processors in Node.js.”)
Cluster: Enables the use of multicore systems. (See Chapter 9.)
Command line options: Gives you Node.js commands to use from a terminal.
Console: Gives the user a debugging console.
Crypto: Allows for the creation of custom encryption. (See Chapter 10, “Using Additional Node.js Modules.”)
Debugger: Allows debugging of a Node.js file.
DNS: Allows connections to DNS servers. (See Chapter 10.)
Errors: Allows for the handling of errors.
Events: Enables the handling of asynchronous events. (See Chapter 4, “Using Events, Listeners, Timers, and Callbacks in Node.js.”)
File system: Allows for file I/O with both synchronous and asynchronous methods. (See Chapter 6, “Accessing the File System from Node.js.”)
Globals: Makes frequently used modules available without having to include them first. (See Chapter 10.)
HTTP: Enables support for many HTTP features. (See Chapter 7, “Implementing HTTP Services in Node.js.”)
HTTPS: Enables HTTP over the TLS/SSL. (See Chapter 7.)
Modules: Provides the module loading system for Node.js. (See Chapter 3.)
Net: Allows the creation of servers and clients. (See Chapter 8, “Implementing Socket Services in Node.js.”)
OS: Allows access to the operating system that Node.js is running on. (See Chapter 10.)
Path: Enables access to file and directory paths. (See Chapter 6.)
Process: Provides information and allows control over the current Node.js process. (See Chapter 9.)
Query strings: Allows for parsing and formatting URL queries. (See Chapter 7.)
Readline: Enables an interface to read from a data stream. (See Chapter 5.)
REPL: Allows developers to create a command shell.
Stream: Provides an API to build objects with the stream interface. (See Chapter 5.)
String decoder: Provides an API to decode buffer objects into strings. (See Chapter 5.)
Timers: Allows for scheduling functions to be called in the future. (See Chapter 4.)
TLS/SSL: Implements TLS and SSL protocols. (See Chapter 8.)
URL: Enables URL resolution and parsing. (See Chapter 7.)
Utilities: Provides support for various apps and modules.
V8: Exposes APIs for the Node.js version of V8. (See Chapter 10.)
VM: Allows for a V8 virtual machine to run and compile code.
ZLIB: Enables compression using Gzip and Deflate/Inflate. (See Chapter 5.)