Christopher Bennage

not all who wander are lost

Node.js on Windows (or JavaScript for the Backend)

What is Node?

The simplest answer, albeit a simplistic answer, is that Node (or Node.js) is JavaScript on the server. Actually, it’s a more than just that, but you can read about the more in other places. This is a good enough answer for us n00bs.

Unless you’ve been living in in cave, you might have noticed that JavaScript is all the rage now. It’s the new assembly language of the Web. (It’s even for the enterprise.) With Node, you can now take that webby clienty goodness to your server applications.

The reason I’m brining all this up is because there’s now a version of Node.js for Windows. It’s currently the unstable release only, but it’s a sign of coolness to come. Furthermore, Microsoft has partnered with Joyent and Rackspace to make it happen. You can read about it here and here. The ultimate goal (according to the posts) is for Node.js to run on both Windows and Azure.

Now, I want to be clear too, since I have been newly assimilated, Node is not a Microsoft product..

Tutorial

I want to show you how easy it is to try out Node.

First, download the exe. I grabbed v0.5.2.

Next, run it. Yeah, it’s that easy. It used to be way harder.

You’ll be presented with a prompt and you can start writing JavaScript in paradigm-shifting REPL.

Now, let’s say you want to create a web server. We’ll begin by yanking the ‘hello world’ snippet from nodejs.org.

var http = require('http');  
http.createServer(function (req, res) {  
  res.writeHead(200, {'Content-Type': 'text/plain'});  
  res.end('Hello World\n');  
}).listen(8124, "127.0.0.1");  
console.log('Server running at <a href="http://127.0.0.1:8124/'">http://127.0.0.1:8124/'</a>);  

I saved this snippet into a file named server.js. Then from a PowerShell prompt, I ran

.node.exe .\server.js

Next, I hit http://localhost:8124 with a browser and I got exactly what you’d expect.

Now you know enough to be dangerous.

Of course, you have to restart Node when you make changes to the file (use Ctrl + C). I’ll show you how I got around that in another post.

Have fun!

Comments