Hello Node.js in 10 minutes

Many think that Node.js is the Next Big Thing in software development. As a way for creating web solutions, it’s got a lot going for it:

  • You write code in familiar javascript (so client and server match)
  • It uses event-driven, non-blocking I/O – which means that it’s really fast for certain types of operations and it’s easier to write certain types of applications
  • It’s open source AND it strives to support Windows as well as Linux

If you’ve been meaning to check it out, now is the time – all you need is 10 minutes to walk through this “hello world” – surely, you’ve got time for that …

Step 1: Install node.js from nodejs.org (I chose the Windows installation)

Step 2: Open cmd, add path, test node

Open a command prompt, and type

path=%path%;"c:Program Files (x86)nodejs"

To make sure that node itself is working, you can evaluate a tiny program right from the command line by typing

node -e "console.log('hey there');"

Step 3: Create, run, and test server

Of course, that’s not what node is really for – it’s really for writing web apps … so create a file called server.js and copy the following into it:

var http = require('http'); 
http.createServer(function (req, res) { 
  res.writeHead(200, {'Content-Type': 'text/plain'}); 
  res.end('Hello Worldn'); 
}).listen(1337, "127.0.0.1"); 
console.log('Server running at http://127.0.0.1:1337/');

This trivial example creates a web server listening on port 1337 of localhost that returns “Hello World” to any request. Run it by typing

node server.js

from the console and then test it by opening a web browser to http://127.0.0.1:1337/

That’s it! 10 minutes from thinking about node to running a simple “hello world” web server.

Soon I’ll get into the kinds of things node.js is good for, but first I want to show how easy it is to deploy node.js to Windows Azure – even if you don’t have a Windows machine (I’ll post that within a couple of days …)

1 thought on “Hello Node.js in 10 minutes

  1. Pingback: Node.js on Windows Azure « Patrick Foley

Leave a Reply

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