Tuesday, April 17th, 2012
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) {
res.writeHead(200);
var userSpan = ‘<span id=”spFirstName”>Prajeesh</span>&nbsp;<span id=”spLastName”>Prathap</span>’
var userHtml = ‘<div id=”pnlLogin”></div>’;
var map = plates.Map();
map.where(‘id’).is(’spFirstName’).use(‘value’).as(‘Prajeesh’);
var content = plates.bind(userHtml, { ‘pnlLogin’: userSpan });
res.end(content)
});
server.listen(8083);


Category: General  | Leave a Comment
Monday, April 16th, 2012
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) {
res.writeHead(404);
res.end(‘Error!!!’);
}
})
});
server.listen(8083);
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) {
this.post(function(id) {
//sayWelcome(’some’);
this.res.writeHead(200, { ‘Content-Type’‘text/plain’ });
this.res.end(‘Create a customer with Id = ‘ + id)
});
this.get(function (id) {
//sayWelcome(’some’);
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)
});
});















Category: General | Tags: ,  | Leave a Comment
Wednesday, April 11th, 2012
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);
process.exit();
});
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);
process.exit();
}
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 {
process.chdir(‘/tmp’);
console.log(‘New directory: ‘ + process.cwd());
}
catch (err) {
console.log(‘chdir: ‘ + err);
process.exit();
}
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’);
process.exit();
}
Category: General  | Leave a Comment
Tuesday, April 10th, 2012
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);
console.log(offset);
//Appending data to the existing buffer
buffer.write(‘. This is an appended text’, offset, ‘utf-8′);
console.log(buffer.toString(‘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);
console.log(helloBuffer.toString(‘utf-8′));
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);
console.log(nodejsSlice.toString());
nodejsSlice.write(‘Prajeesh’);
console.log(helloBuffer.toString());


Category: General | Tags:  | Leave a Comment
Monday, April 09th, 2012
Socket.io 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 socket.io on the server
Creating the server
var http = require(‘http’),
io = require(’socket.io’),
sys = require(‘util’),
express = require(‘express’);
var port = 8083;
var server = express.createServer(function (req, res) {
res.writeHead(200, { ‘Content-Type’: ‘text/html’ });
res.end(‘<h1>Socket server…</h1>’);
});
server.listen(port);
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) {

client.send(message);
}, 500, ‘From server…’);
});
Client code
<!DOCTYPE html>
<html>
<head>
<script src=”//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”></script>
<script src=”socket/socket.io.js”></script>
<script>
var socket = new
io.Socket();
socket.connect(‘http://localhost:8083′);

socket.on(‘connect’, function () {

$(‘#status’).text(‘Connected’);
});

socket.on(‘message’, function (data) {

$(‘#message’).text(data);
});

socket.on(‘disconnect’, function () {

$(‘#status’).text(‘Disconnected’);
});

socket.on(‘customEvent’, function (message) {

$(‘#customEvent’).text(message['time']);
});
</script>
</head>
<body>
<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>
</body>
</body>
Category: PHP  | Leave a Comment
Friday, April 06th, 2012
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) {
console.log(timeoutMessage);
}, 100, ‘Hello timeout’);
var intervalId = setInterval(function (message) {
index++;
if (index == 5) {
clearInterval(intervalId);
}
console.log(message);
}, 200, ‘Hello interval’);


Category: General  | Leave a Comment
Thursday, April 05th, 2012
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 easiestway 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);
}

console.log(data);
});


Category: General  | Leave a Comment
Wednesday, April 04th, 2012
Modifying a cursor
The Find method doesn’t immediately return the actual
results of a query. Instead they return a cursor (MongoCursor) that can be enumerated to retrieve the results
of the query. The query is sent to the server when we first try to retrieve the
result. This feature allows the user to control the results of the query before
fetching the results by modifying the cursor.
For e.g. you can use the skip, limit, sort properties to
modify the cursor. You can also use the fluent interface methods for the
properties to modify the cursor. After setting these properties, you can
enumerate the results to get the actual output.
[TestMethod]
public void
SkipAndLimitShouldReturnPagedDataFromACursor()
{
var database = GetDatabaseInstance();
var collectionSettings =
database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var employeeCursor = employees.FindAll();
employeeCursor.Skip = 5;
employeeCursor.Limit = 2;
Assert.IsTrue(employeeCursor.ToList().Count == 2);
}
[TestMethod]
public void
SkipAndLimitUsingFluentInterfacesShouldReturnPagedDataFromACursor()
{
var database = GetDatabaseInstance();
var collectionSettings =
database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees =
database.GetCollection(collectionSettings);
var employeeCursor = employees.FindAll().SetSkip(5).SetLimit(2).SetSortOrder(“FirstName”, “LastName”);
Assert.IsTrue(employeeCursor.ToList().Count == 2);
}
Category: .Net, General  | Leave a Comment
Tuesday, April 03rd, 2012

Changing values in a collection

You can update/ save values in a collection using the Save, Update or FindAndModify methods. The Save method is a combination of Insert and Update. If the Id member of the document has a value, then it is assumed to be an existing document and Save calls Update on the document. Otherwise it is assumed to be a new document and Save calls Insert after first assigning a newly generated unique value to the Id member.
[TestMethod]
public void SaveShouldSaveTheChangedEntityToCollection()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var query = Query.And(Query.EQ(“FirstName”“Betty”), Query.EQ(“LastName”, “Green”));
var employee = employees.FindOne(query);

employee.FirstName = “Rachel”;
employees.Save(employee);
query = Query.And(Query.EQ(“FirstName”, “Rachel”), Query.EQ(“LastName”“Green”));
employee = employees.FindOne(query);
Assert.IsTrue(employee != null && employee.FirstName == “Rachel”);
}
The Update method is used to update existing documents.
[TestMethod]
public void UpdateShouldUpdateTheChangedEntitiesMacthingTheQueryBuilderConditionToCollection()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var query = Query.And(Query.EQ(“FirstName”“Rachel”), Query.EQ(“LastName”“Green”));
employees.Update(query, Update.Set(“FirstName”“Betty”));
query = Query.And(Query.EQ(“FirstName”, “Betty”), Query.EQ(“LastName”“Green”));
var employee = employees.FindOne(query);
Assert.IsTrue(employee != null && employee.FirstName == “Betty”);
}
FindAndModify always updates a single document, and you can combine a query that matches multiple documents with sort criteria that will determine exactly which matching document is updated. In addition, FindAndModify will return the matching documentsand if you wish you can specify which fields of the matching document to return.
[TestMethod]
public void FindAndModifyShouldFindAndModifyTheCollectionInAtomicOperation()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var query = Query.Or(Query.EQ(“FirstName”“Mike”), Query.EQ(“FirtName”, “Steve”));
var sortOrder = SortBy.Ascending(“FirstName”“LastName”);
var update = Update.Set(“DoJ”new DateTime(1995, 8, 1));
var updatedEmployees = employees.FindAndModify(query, sortOrder, update, true);
var employeesModified = updatedEmployees.GetModifiedDocumentAs<Employee>();
Assert.IsTrue(employeesModified.DoJ.Date.Year == 1995);

}

Category: .Net, General | Tags: ,  | Leave a Comment
Tuesday, April 03rd, 2012

Filtering a collection

To retrieve documents from a collection use one of the various Find methods. FindOne returns the first document it finds (when there are many documents in a collection you can’t be sure which one it will be).
[TestMethod]
public void
FindOneShouldReturnTheFirstEntryInTheCollection()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var firstEmployee = employees.FindOne();
Assert.IsTrue(firstEmployee.Id != default(int));
}
If you want to read a document that is not of the

type use the FindOneAs method, which allows you to override the type of the returned document.

[TestMethod]
public void
FindOneAsShouldReturnTheFirstItemInTheCollectionOverridingTheTypeOfReturnedDocument()
{
var database = GetDatabaseInstance();
var employees = database.GetCollection(“Employees”);
var employee = employees.FindOneAs(typeof (Employee));
Assert.IsTrue(employee != null && employee is Employee);
}
The Find and FindAs methods take a query that tells the server which documents to return. The query parameter is of type IMongoQuery. IMongoQuery is a marker interface that identifies classes that can be used as queries. The most common ways to construct a query are to either use the Query builder class.
[TestMethod]
public void
FindOneShouldReturnTheFirstItemInTheCollectionBasedOnQuery()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var queryDocument = new QueryDocument(“FirstName”“Betty”);
var employee = employees.FindOne(queryDocument);
Assert.IsTrue(employee != null && employee.FirstName == “Betty”);
}
[TestMethod]
public void
QueryBuilderCanBeUsedToFilterEntriesInCollection()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var query = Query.And(Query.EQ(“FirstName”“Betty”), Query.EQ(“LastName”, “Green”));
var employee = employees.FindOne(query);
Assert.IsTrue(employee != null && employee.FirstName == “Betty”);
}
[TestMethod]
public void
FindReturnsAllEntriesMatchingTheQueryInTheCollection()
{
var database = GetDatabaseInstance();
var collectionSettings = database.CreateCollectionSettings<Employee>(“Employees”);

collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var query = Query.Or(Query.EQ(“FirstName”“Betty”), Query.EQ(“FirstName”, “Prajeesh”));
var employeeResult = employees.Find(query);
Assert.IsTrue(employeeResult.All(e => e.FirstName == “Betty” || e.FirstName == “Prajeesh”));
}
Category: .Net, General  | Leave a Comment