MSDN UK Team blog

Get all the latest Microsoft developer news, tools, downloads, tutorials and tips right here, every day.
Search Form
Unfiltered HTML
Developers code with microsoft

Featured Article: Hello Node

Unfiltered HTML
Unfiltered HTML

Featured Article: Hello Node

Rate This
  • Comments 3

Daniel ElliotLast week I also 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. As a bit of a break from Microsoft technical articles 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? Would you be willing to help port it to Windows? Probably more importantly - do you think it’s worthwhile discussing technologies like this on the MSDN flash? 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

var http = require('http');
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello node!\n");
}).listen(8888);
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

Additionaly 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:

Call To Arms

So why an article on Node in the MSDN Flash? Firstly (and to give credit where credit is due), Martin Beeby asked me to write it as he thought it would interest the readership. Secondly, we need Node or its like in the Windows world. Work is continuing on porting Node to Windows. If you have the time, the inclination and the relevant expertise, please consider getting involved!

  • If you follow the Linux Machine or VM link and scroll down to step 3b, there is a link to some pre-built Windows binaries.  I've used these and have been able to get simple demo programs to run.

  • Agree with Dunx, I have node.js working using the standalone binaries, great thing is, you can use VS2010 & take advantage of Intellisense :)

  • I would also say, yes, this sort of thing is of interest & is the best solution I have see so far for implementing proper WebSockets over port 80 (node just upgrades to the same port the web server is on, all the others seem to require you to perform the upgrade to a different port.

Page 1 of 1 (3 items)
Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post