Omarrr

Getting started with Node.js

Easy as pie install of node.js.

Although Node.js is frequently mentioned as a backend alternative to Ruby on Rails it is neither a language nor a framework. Node is an asynchronous I/O library build on V8 (Google’s open source JavaScript engine). Express.js is a framework built on top of node.js and it’s a better comparison to RoR than node itself.

nodejs

From Node’s site:

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.

Installing node (& npm)

Donwload (on Mac get PKM) from http://nodejs.org/#download & double click easy installer :)

Testing node

Write a hello world example: hi.js’ (from nodejs)

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/');

Execute

$ node hi.js
Server running at http://127.0.0.1:1337/

Goto http://127.0.0.1:1337/ to check out the results

No markup whatsoever. Inspecting the source code of the page will get simply:

Hello World

Done and done.

REPL

REPL (Read-Eval-Print-Loop) is an interactive shell for node that can be invocated by simply calling node with no app.js to execute. It works just like the debug console in Chrome.

$ node

> 2+3
5
> console.log('hi');
hi
undefined
> ["Hello","Word"].join();
'Hello,Word'
> 

What’s next

Testing express.js, a framework over node, installing brunch, an app assembler and scafolding tool build on top of node, integrating node backend with a front-end MV* framework such as backbone.js, etc, etc, etc…

July 4, 2012 ☼ codeinstalljavascriptnode jsnodejsnpmrailsrorrubyruby-on-railsv8