Wednesday, January 4, 2012

Giving Node.js a Try

Based upon their own description: "Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices." (-http://nodejs.org/)

Seems pretty tempting. In particular I'm interesting is using Node.js for Javascript unit testing in combination with Jasmine. As I've started creating some basic script libraries for dependency injection and publish-subscribe I need a way to unit test and a way to run these tests. It's also promising for use with JsRender as well for templating on the server-side.

This blog post (5 reasons to give node.js some love) covers what I think are the basic reasons why to give it a try:
1) You get to write Javascript on the server and the client
2) Event-based asynchronous stuff is just How Things Work in JavaScript
3) Awesome package management!
4) Community community community
5) It's easy, really easy, like super easy, We can all do it!

(-http://codeofrob.com/archive/2011/04/30/5-reasons-to-give-node-js-some-love.aspx)

The download link on the Node.js site offers three download options: Windows Installer, Mac Installer, and source. Today I am going to download both the Windows Installer and the source (to take a look). For reference the project is hosted on GitHub here: https://github.com/joyent/node. Taking a look, the project seems active and had a recent check in (43 minutes ago).

After running the installation, getting started is easy.
1. Open a command-prompt
2. Navigate to the executable path (on my machine): "C:\Program Files (x86)\nodejs\"
3. Create a small, sample script to be executed, and name it example.js. In my case I copied it from the example:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
4. Type node example.js
5. node.js will respond with the result of the executed script. In this case:
"Server running at http://127.0.0.1:1337/"

How to exit node.js:
To exit node.js you have to enter the key-combination Ctrl-C twice or Ctrl-C, Ctrl-C

References:
Node.js
Jasmine
JsRender
5 reasons to give node.js some love

No comments: