HTML and CSS Reference
In-Depth Information
Chapter 8
Running JavaScript on the Command Line
What's in this chapter?
• Installing a server JavaScript environment
• Understanding Node.js
• Installing and using Node modules
• Writing your own Node command-line module
Wrox.com Code Downloads for this Chapter
The wrox.com code downloads for this chapter are found at www.wrox.com/rem-
title.cgi?isbn=9781118301326 on the Download Code tab. The code is in the chapter 08 download and individu-
ally named according to the names throughout the chapter.
Introduction
JavaScript has always been predominantly used as a browser-based language throughout its history. Although it
has had forays into the server side dating back to Netscape's LiveWire in 1996, JavaScript on the server never
seemed to gain much traction. This started to change in 2010 following a November 2009 JSConf presentation
on Node.js. Suddenly—and almost inexplicably—people were interested in running JavaScript separate from the
browser. In the intervening years JavaScript has turned into a perfectly acceptable language to use on the server
and the command line. This chapter will show you how to install, run, and write JavaScript from the command
line to support your game.
Learning About Node.js
Other server-side JavaScript environments are available; however, Node.js (commonly now just referred to as
Node ) is by far the most popular and has the best cross-platform support (Windows, OS X, and Linux). Because
of this popularity, Node is the JavaScript environment discussed in this topic.
One of the core ideas of Node is that all I/O should be nonblocking. This means any time the server waits
on some data or some input, it shouldn't prevent the execution of other code. The way that Node handles this is
through the use of callbacks , which are executed automatically when the resource that was blocking becomes
available. This is often referred to as evented programming . It's something that JavaScript programmers are
familiar with because it's the way that most user interfaces (including web pages) are programmed: You add an
event listener with a callback that gets called when that event is triggered.
Search WWH ::




Custom Search