HTML and CSS Reference
In-Depth Information
The reason it's essential to write nonblocking code in Node is that Node is single-threaded, meaning that
there is only a single thread of execution at a given time. If the code stalls for any reason, the entire server stops
taking requests. This might seem like a disadvantage, but by being single-threaded, Node can handle a huge
number of concurrent requests without massive amounts of memory usage. Being single-threaded also means
that Node doesn't need to switch contexts between threads, which provides a performance boost. Combine this
with the speed of the underlying JavaScript engine V8 (the engine that powers Chrome), and you can see why
people are excited. A language that people decried as a kiddie scripting language for years suddenly is beating
server-side language stalwarts such as Python, PHP, and Ruby in benchmarks.
Server-side JavaScript runs outside of the browser. You might want to do this for two primary reasons. The
first is to write a server that can handle web requests. The second is to write command-line scripts that can auto-
mate certain tasks. This topic discusses both uses.
This chapter discusses using Node to write command-line scripts in JavaScript that do stuff such as lint your
code and package and minify your JavaScript. Chapter 18, “Creating a 2-D Platformer,” and Chapter 19, “Build-
ing a Canvas Editor,” discuss building game servers using Node, something that an evented single-threaded
server does well.
Installing Node
With the release of Node 0.6, easy installation on Windows became a reality. Previously you needed to install
a UNIX-style POSIX environment such as Cygwin to get Node to run. Since 0.6, Node comes with installer
packages for Windows and OS X available at .
You can run the installer package and follow the prompts to get Node up and running, unless you use Win-
dows, in which case you have no other option. The installer isn't the ideal method to install node, as it won't set
up a development environment alongside it. Without this development environment, you won't be able to install
modules with native C and C++ code. For this reason, you should follow these instructions for your specific
Installing Node on Windows
On Windows, the only current option is to install Node from a package or compile with Visual Studio. You will,
unfortunately, have difficulty installing modules that have native source code that needs to be compiled (such
as the node-canvas module used in a later section).
As of this writing, there isn't an up-to-date, prebuilt node-canvas Windows library, so to follow along with
the tutorial in the next section that uses node-canvas , you can download VMWare's VMPlayer software at . VMPlayer is a free piece of software that enables you to run a virtual Linux
computer from inside of Windows. You can download a Linux image from .
Find the Ubuntu Desktop image (not the server image) with the largest number (11.10 as of this writing),
download it, and run it from VMPlayer. When that machine is up and running inside of VMPlayer, you can
launch the command line from Applications > Accessories > Terminal. From there follow the Linux installation
instructions. To copy a file into the Linux virtual machine, you can just drag it from your desktop onto the ma-
chine and it will add it to the desktop.
Search WWH ::

Custom Search