So let’s get down to brass tacks here. You don’t have much time. You’re probably sipping a cup of the office sludge or tossing one back at the local watering hole. Either way, I’ve got your attention for five minutes at best.
“Four minutes and thirty seconds, get on with it already.”
Right, well, let’s start simply. You probably already know that Node is a server-side javascript environment built on top of the V8 engine. You may even know that Node is an event-driven environment similar to EventMachine in Ruby, or Twisted in Python. What you probably don’t know is that Node is not magic.
“No! Say it ain’t so! You mean it can’t pull a rabbit out of my-”
Hat? No. Node is the result of a lot of hard work, sweat, and what I can only imagine to be the blood of Unicorns.
“But you just said Node isn’t magic. Unicorns are magical…you’re losing me here.”
Right, well, what I’m getting at is that Node doesn’t pull its power from the ether. Everything contained within was built from scratch, including the various modules available such as HTTP and TCP transport. It also means that anything not available out-of-the-box must be written from scratch as well, and that includes things like database drivers. Luckily, the community surrounding Node is very active and the magic, supporting modules we take for granted are being written as we speak.
“Ok, so Node isn’t magic. What is it then?”
Well, let me start with what it is not. Node is not a web framework like Rails or Django or ASP.NET MVC. Node is not a general purpose web server. Node is not just for HTTP, it can leverage TCP, or be used as a general purpose scripting environment. Node is not just an HTTP server, it’s also an HTTP client.
“Did I not just ask you what Node is? You just told me a bunch of things that Node isn’t. Did you get confused and forget?”
I didn’t forget, I’m just being thorough. As I said, Node isn’t magic. Node is a Lego set. Node gives you the little bricks you need to build highly scalable event-driven web applications. It’s simple, easy to assemble, and comes with instructions if you want them.
“Can I buy the space shuttle set!?”
Sure! Sort of…if you’re making some sort of comparison between lego sets and frameworks built on Node. We’ll cover them later on in this series, but for now just know that there are full web application frameworks in the wild based on Node. Let’s just start playing with the Legos we already have.
var HTTP = require('HTTP');
HTTP.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("I'm a space shuttle!\n");
}).listen(1337, "127.0.0.1");
console.log("Server running at http://127.0.0.1:1337/");
“That’s the worst looking space shuttle I’ve ever seen.”
Everyone’s a critic. Before you pass judgement, at least let me explain how it works. Let’s start with the bottom piece, err, the first line. The important part is the call to require(). The require function imports javascript from another file. In this case, we’re importing Node’s HTTP module. So we’re effectively throwing the HTTP lego kit into the mix. Our next call is to the createServer function which, as you might imagine, creates an HTTP server instance.
“Your ability to state the obvious knows no bounds.”
Continuing on stating the obvious then, Mr. Smartypants, we immediately register a callback for any requests made to the server. Upon any request, we set the content-type to plain text and write “I’m a space shuttle!” to the response. We then tell our server object to listen on port 1337 and write a message to the console.
“Oh look at the 1337-h4x0r here. Pandering to the rock-star, script-kiddie masses are we?”
You know what? I’m taking my space shuttle and leaving. If you save our space shuttle script to a file and run it under Node:
$ node shuttle.js
Your browser will print “I’m a space shuttle!” if you browse to http://localhost:1337/.
Now that you’ve been given a silly introduction to Node, I’m going to busy myself with improving our space shuttle. Next time I’ll show off a proper example that emits a real web page, rather than just plain text. I’d also like to share more about what makes Node special, but I don’t want to hit you with too much technical mumbo-jumbo all at once.
“Good, all your hot air is fogging up my monitor.”
Honestly, where is your off switch…
-
http://www.alvinashcraft.com/2011/09/28/dew-drop-september-28-2011/ Dew Drop – September 28, 2011 | Alvin Ashcraft's Morning Dew
-
Vijay T
-
Drew Peterson


