Monday, 16 April 2012

Node.js: Templating with Plates

Plates is the templating library in flatiron. Plates binds data to markup. It's Javascript, Markup and JSON. It works in the browser and in node.js. All templates are actually valid HTML, with no special characters for value insertion. The relationship between tags and values is defined through object literals that are passed to Plates.bind:
You have to install plates before using it

Plates can be used in the program by requiring it.
var http = require('http'),
    plates = require(../lib/plates');

var server = http.createServer(function (req, res) {
    var userSpan = '<span id="spFirstName">Prajeesh</span>&nbsp;<span id="spLastName">Prathap</span>'
    var userHtml = '<div id="pnlLogin"></div>';

    var map = plates.Map();
    var content = plates.bind(userHtml, { 'pnlLogin': userSpan });


Sunday, 15 April 2012

Routing using director in Node

Director is a URL router module which comes as part of the flatiron framework.  It works in a browser for single page apps and in Node.js. It's not a plugin for another framework. It's not dependent on anything. It's a modern router that was designed from the ground up with javascript.
Installing flatiron
You can install flatiron using npm (npm install flatiron –g)

Director handles routing for HTTP requests similar to journey or express:
var http = require('http');
var director = require(../lib/director');

function sayWelcome(route) {
    this.res.writeHead(200, { 'Content-Type': 'text/plain' });
    this.res.end('Welcome to flatiron routing sample');

var router = new director.http.Router({
    '': {
        get: sayWelcome

router.get('/index', sayWelcome);
router.get('/home', sayWelcome);

var server = http.createServer(function (req, res) {
    router.dispatch(req, res, function (err) {
        if (err) {

When developing large client-side or server-side applications it is not always possible to define routes in one location. Usually individual decoupled components register their own routes with the application router. Director supports ad-hoc routing also.
router.path(/\/customers\/(\w+)/, function (res) { (id) {
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Create a customer with Id = ' + id)

    this.get(function (id) {
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Gets a customer with Id = ' + id)
    this.get(/\/orders/, function (id) {
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Gets orders for a customer with Id = ' + id)

Tuesday, 10 April 2012

Exception handling in Node

A common problem when writing Node.js programs is in ensuring all exceptions are handled. Node.js will helpfully die when exceptions are not caught and handled, which forces you, the author, to ensure robustness in your Node.js applications.  Node emits an uncaughtException event when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur.
process.on('uncaughtException', function (ex) {
    console.log('Unhandled exception caught: ' + ex);

If you are writing asynchronous code, you can follow the below given pattern to throw and handle exceptions in the code. In this pattern the first argument of the callback is err, if an error happens it is the error, if it doesn't it is null.
var divideAsync = function (arg1, arg2, callback) {
    if (arg2 == 0) return callback(new Error('division by 0 is not possible'));
    return callback(null, arg1 / arg2);

divideAsync(9, 0, function (ex, result) {
    if (ex) {
        console.log('Error in division ' + ex);
    console.log('result ' + result);

You can also use the traditional try {} catch (err) {} to catch exceptions as given below
console.log('Starting directory: ' + process.cwd());
try {
    console.log('New directory: ' + process.cwd());
catch (err) {
    console.log('chdir: ' + err);

For synchronous code, if an error happens you can return an instance of Error object and later check the result for this object as given below.
var divide = function (arg1, arg2) {
    if (arg2 == 0) return new Error('division by 0 is not possible');
    return arg1 / arg2;

var x = divide(5, 0);
if (x instanceof Error) {
    console.log('Exception logged');

Monday, 9 April 2012

Buffers in Node – Part 1

Pure javascript, while great with unicode-encoded strings, does not handle straight binary data very well. This is fine on the browser, where most data is in the form of strings. However, node.js servers have to also deal with TCP streams and reading and writing to the filesystem, both which make it necessary to deal with purely binary streams of data. Node has several strategies for manipulating, creating, and consuming streams.

Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

The Buffer class is a global, making it very rare that one would need to ever require('buffer').

Converting between Buffers and JavaScript string objects requires an explicit encoding method. The encodings that are supported are 'ascii', 'utf8', 'usc2', 'base64', 'binary' and 'hex'.
Writing to buffers

new Buffer(size) methods allocates a new buffer of size octets. Once the buffer is created, the buffer.write method is used to write a string value to the buffer

var buffer = new Buffer(100);
var stringToWrite = 'Sample text to add to a buffer';
//Write data to a buffer
buffer.write(stringToWrite, 'utf-8');
//finding the length of buffer
var offset = Buffer.byteLength(stringToWrite);
//Appending data to the existing buffer
buffer.write('. This is an appended text', offset, 'utf-8');

Copy values to buffer
buffer.copy allows one to copy the contents of one buffer onto another. The first argument is the target buffer on which to copy the contents of buffer, and the rest of the arguments allow for copying only a subsection of the source buffer to somewhere in the middle of the target buffer. For example:
var helloBuffer = new Buffer(50);
helloBuffer.write('Hello ', 0, 'utf-8')
var welcomeBuffer = new Buffer('node.js.');
var helloStringLength = Buffer.byteLength('Hello ');
var welcomeStringLength = Buffer.byteLength('node.js.');
//Copies from welcomeBuffer to helloBuffer
welcomeBuffer.copy(helloBuffer, helloStringLength, 0);

Cropping the buffer
buf.slice([start], [end]) method returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. The slice is not a new buffer and merely references a subset of the memory space. Modifying the slice will also modify the original buffer. For example:
var nodejsSlice = helloBuffer.slice(helloStringLength, helloStringLength + welcomeStringLength);

Sunday, 8 April 2012

Node.js application using provides real-time communication between your node.js server and clients. Socket.IO allows you to emit and receive custom events. Besides `connect`, `message` and `disconnect`, you can emit custom events also.
The socket can be created by the user and used as a client (with connect()) or they can be created by Node and passed to the user through the 'connection' event of a server.

Installing on the server

Creating the server
var http = require('http'),
    io = require(''),
    sys = require('util'),
    express = require('express');

var port = 8083;
var server = express.createServer(function (req, res) {

    // Send HTML headers and message
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('<h1>Socket server...</h1>');

var socket = io.listen(server);

socket.sockets.on('connection', function (client) {
    client.on('message', function (data) {
        console.log('Message received: ' + data);
    client.on('disconnect', function () {
        console.log('Disconnected from server!!!');
    var intervalId = setInterval(function (message) {
    }, 500, 'From server...');

Client code
<!DOCTYPE html>
    <script src="//"></script>
    <script src="socket/"></script>
        var socket = new io.Socket();

        socket.on('connect', function () {
        socket.on('message', function (data) {
        socket.on('disconnect', function () {
        socket.on('customEvent', function (message) {
    <h1>WebSocket Test</h1>
    <h2>Status: <span id="status">Waiting</span></h2>
    Received using client.send <pre id="message"></pre>
    <hr />   
    Received using client.emit <pre id="customEvent"></pre>

Thursday, 5 April 2012

Timers in Nods.js

You can use the JavaScript timers, such as setTimeout(), clearTimeout(), setInterval(), clearInterval() in your node apps. All of the timer functions are globals. You do not need to require() this module in order to use them.
To schedule the repeated execution of callback every delay milliseconds you can use the setInterval() method. The setInterval returns an intervalId which can be later used to clear the interval execution with the clearInterval() method.
Similary to schedule execution of a one-time callback after delay milliseconds use the setTimeout() method.
var index = 0;

setTimeout(function (timeoutMessage) {
}, 100, 'Hello timeout');

var intervalId = setInterval(function (message) {
    if (index == 5) {
}, 200, 'Hello interval');

Wednesday, 4 April 2012

File I/O in Node

The fs module provides both synchronous and asynchronous ways of reading files. The readFile method reads the contents of the file asynchronously.
fs = require('fs');
fs.readFile(file, [encoding], [callback]);

where file is the name of the file to read, encoding is an optional parameter that specifies the type of encoding to read the file. Possible encodings are 'ascii', 'utf8', and 'base64'. If no encoding is provided, the default is utf8.callback is a function to call when the file has been read and the contents are ready - it is passed two arguments, error and data. If there is no error, error will be null and data will contain the file contents; otherwise err contains the error message.
Similarly the writeFile method writes content to a file specified in the function.
var fileSystem = require('fs');

var textToWrite = 'The easiest way to read the entire contents of a file is with fs.readFile, fs.readFile(file, [encoding], [callback]); encoding is an optional parameter that specifies the type of encoding to read the file. Possible encodings are \'ascii\', \'utf8\', and \'base64\'';
textToWrite = textToWrite + '. If no encoding is provided, the default is utf8. callback is a function to call when the file has been read and the contents are ready - it is passed two arguments, error and data. If there is no error, error will be null and data will contain the file contents; otherwise err contains the error message.';

fileSystem.writeFile(__dirname + '/HowToRead.txt', textToWrite, function (error) {
    if (error) {
        console.log('Error writing file!!!' + error);

fileSystem.readFile(__dirname + '/SampleText.txt', 'utf8', function (error, data) {
    if (error) {
        console.log('Error reading file!!!' + error);

Sunday, 1 April 2012

Node.js: EventEmitter and Observer pattern

The EventEmitter is the base building block for all compositions that would need to broadcast data to multiple consumers. Whenever there is an object that represents the source of several kinds of events, node.js usually makes the underlying class inherit from EventEmitter. The EventEmitter implementation makes use of the observer pattern by providing the ‘on’ function for objects that are interested in listening to an event. For e.g. you can listen to a specific event by calling the 'on()' function on your object, providing the name of the event, as well as a callback closure as the parameters.
eventEmitterInstance.once('event', callbackSample1); //singular callback registration
eventEmitterInstance.on('event', callBackSample2); //multiple callback

Below given is a sample server implementation that logs the messages passed as querystring to the server using the EventEmitter implementation

var http = require('http');
var port = process.env.PORT;

var server = http.createServer(function(request, response){
    var eventEmitter = require('events').EventEmitter;
    var eventEmitterInstance = new eventEmitter();
    var querystring = require('querystring');
    var message = querystring.parse(request.url).message;
    var callbackSample1 = function(message)
        console.log('logging from 1st callback function ' + message);
    var callBackSample2 = function(message)
        console.log('logging from 2nd callback function ' + message);
    eventEmitterInstance.once('event', callbackSample1); //singular callback registration
    eventEmitterInstance.on('event', callBackSample2); //multiple callback
    eventEmitterInstance.removeListener('event', callBackSample2);
                        '<meta http-equiv="Content-Type" content="text/html; '+
                        'charset=UTF-8" />'+
                    '<form method="post">' +
                         '<h1>Messaging sample </h1>' +
console.log('Server started....');

As you can see, the on() function returns a reference to the object it belongs to, allowing you to chain several of such event listeners.  If you're only interested in the first occurrence of an event, you can use the once() function instead. Finally, you can remove event listeners by using the removeListener function.