So what is node?

node logoTwo weeks ago I attended and spoke at the Web Directions conference in London. In one of the sessions Douglas Crockford mentioned that Yahoo! is considering switching all of their server-side code from PHP to JavaScript. In particular he talked about using node.js, stating that he felt it was nearly ready for large production sites. His talk was interesting and being a newbie to node I thought I’d as ask a good friend of mine Daniel Elliot to explain what server-side JavaScript is all about, and also see what we could do if we wanted to get node.js on Windows. So what do you think about node.js? Please let me know in the comments. Over to Dan:

Node.js (or just Node) is, in short, JavaScript on the server. "Big Deal!" I hear you say, "We could use JScript in Classic ASP on the server almost a decade ago!" Correct! So trying again, Node is JavaScript on the server done right. According to its website, Node is evented I/O for V8 JavaScript. Whilst accurate, that description in itself needs a little describing.

What is Node.js?

Let’s start with V8. V8 is the JavaScript implementation used within Chrome. It utilises "Just In Time" compilation to achieve performance that was previously unattainable in JavaScript. In fact, these improvements lift V8 JavaScript into the same realms of performance as Clojure, Java or Go.

Evented (or Asynchronous) I/O is one of the ways that we can deal with concurrent requests to a network resource (such as a website.) A webserver like Apache would spawn new threads to service the multiple concurrent requests it received. Evented I/O systems such as nginx, Thin or Node, utilise a single non-blocking thread in conjunction with an event loop.

JavaScript was designed to be used in a single threaded, event-looped based environment (your browser!) and hence lends itself very well to this sort of server setup. Whenever we need to call a function that requires some kind of I/O, we pass it a callback and JavaScript’s natural creation of closures ensures that we maintain the necessary scope to service that callback once the I/O has been completed.

Installing Node

Enough talk of all this goodness, let’s install it. Oh, time for a little bit of bad news. At the time of writing Node does not run natively under Windows. So what now? If you are a Windows user, you have two options. The first is cygwin, this gives you a bash shell(think command prompt) that is capable of hosting Node. Second is to virtualise a Linux machine.

I am a Windows developer by day; I spend way too much time in Visual Studio and find a great deal of satisfaction within its confines. However, I cannot stress enough how much I have learnt about the underlying tenets of our industry by thinking outside of my Windows 7 box. By experiencing the slightly more raw side of computing under Linux, I am a better all-round geek. To this end, I’d strongly suggest you plump for the second option. Virtual PC, VirtualBox or Hyper-V all do a good job of hosting Linux VMs under Windows.

Both installation options are well covered in Node’s documentation:

Once you get to the end of the make install command you can assert that Node is installed correctly by running the commands below. Ctrl-C will exit the Node prompt when you are done.

prompt> node
 > 2 + 2
 4
 >

Now we have a shiny new Node install, let’s write the obligatory "Hello World" example.

Hello Node

Here is a very simple web server written for Node.

hellonode.js

1
var http = require('http');<br />http.createServer(function (req, res){<br />    res.writeHead(200, {'Content-Type': 'text/plain'});<br />    res.end(&quot;Hello node!\n&quot;);<br />}).listen(8888);<br />console.log('Running a simple web server at http://localhost:8888');

Let’s have a look at what is actually happening here line by line.

The first line is akin to a C# using or Java import, it tells us that we "require" Node’s http module and we will set it to a variable http. It should be noted that requires are the only blocking instructions in Node (as it must load the dependencies before continuing execution.)

The next statement is the whole web server! We call the createServer method of http. It takes a function with two parameters (the request and response) as its argument. We have chosen to have ours write the content type to the header and respond "Hello node!" to each request. Finally we have chained a call to the listen method which instructs the created server to listen on port 8888 for requests.

The final log statement is there only as an assertion that the script has run without error until that point.

Save this file as hellonode.js and run the following command

node hellonode.js

Now pointing a browser at http://localhost:8888 returns "Hello node!"

Taking It Further

So what now? Well anything really! Node has a package manager (npm) that allows you to install modules that have been written by the community to extend Node’s functionality. Issuing the following command will install npm.

curl http://npmjs.org/install.sh | sh

Additionally I’d suggest giving the resources listed below a peruse, in particular HowToNode.org is a wealth of information.

Resources

Video:

  • Ryan Dahl (creator of Node) gives a wonderful introduction to node

Sites:

Published by thebeebs

Thebeebs is a Canadian pop singer, songwriter, actor and HTML5 junkie. Throughout his rise to fame, Thebeebs has been nominated and awarded numerous accolades, winning Artist of the Year at the 2010 American Music Awards, and being nominated for Best New Artist and Best Pop Vocal Album at the 53rd Grammy Awards. Thebeebs is considered a teen idol, and has been subject to acclaim from fans, as well as criticism and controversy from matters concerning his popularity and image.

3 Comments So Far, what do you think?

  1. Pingback:Node.Js coming to Windows - Ubelly

  2. Pingback:node.js » Melis Özmen

  3. Pingback:Introduction to Node.js | erman taylan

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>