Tuesday, April 17, 2012

Node.js: Templating with Plates

Plates is the templating library in flatiron. Plates binds data to markup. It's JavascriptMarkup 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: Read more..

Monday, April 16, 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. Read more..

Wednesday, April 11, 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. Read more...

Tuesday, April 10, 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'. Read more...

Monday, April 9, 2012

Node.js application using Socket.io


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. Read more...

Friday, April 6, 2012

Timers in Node.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. Read more...

Thursday, April 5, 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. Read more

Wednesday, April 4, 2012

C# and MongoDb tips – Part 4


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);
}

Tuesday, April 3, 2012

C# and MongoDb tips – Part 3


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);
}

C# and MongoDb tips – Part 2


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"));
}

Sunday, April 1, 2012

C# and MongoDb tips – Part 1


Establish a server instance
Create method is used to obtain an instance of MongoServer by passing a valid connection string:
[TestMethod]
public void CreateMethodCreatesOpensAServerConnection()
{
    const string connectionString = "mongodb://localhost";
    var server = MongoServer.Create(connectionString);

    Assert.IsTrue(server.DatabaseExists("test"));
}
Connecting to a database
You can navigate from an instance of MongoServer to an instance of MongoDatabase  using one of the GetDatabase method
private static MongoServer GetMongodbServer()
{
    const string connectionString = "mongodb://localhost";
    var server = MongoServer.Create(connectionString);
    return server;
}

[TestMethod]
public void CreateDatabaseShouldOpenADatabaseWithTheCredentialsPassed()
{
    var server = GetMongodbServer();
    var databaseSettings = server.CreateDatabaseSettings("sampleDb");
    databaseSettings.SlaveOk = true;
    databaseSettings.Credentials = new MongoCredentials("admin", "pass@word1");
    var database = server.GetDatabase(databaseSettings);
    Assert.IsTrue(database.Name == "sampleDb");
}

Inserting values to a collection
To insert a document in the collection create an object representing the document and call Insert. The object can be an instance of BsonDocument or of any class that can be successfully serialized as a BSON document. You can insert more than one document at a time using the InsertBatch method.
[TestMethod]
public void InsertAndInsertBatchShouldInsertValuesToACollection()
{
    var database = GetDatabaseInstance();
           
    var server = database.Server;

    using (server.RequestStart(database))
    {
        if (database.CollectionExists("Employees"))
            database.DropCollection("Employees");
               
        database.CreateCollection("Employees");

        var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
        collectionSettings.SlaveOk = true;
        var employees = database.GetCollection(collectionSettings);

        var employeesToAdd = new List<Employee>
                                {
                                    new Employee {Id = 1, FirstName = "Mike", LastName = "Pagel", DoJ = new DateTime(1990, 10, 1)},
                                    new Employee {Id = 2, FirstName = "Steve", LastName = "John", DoJ = new DateTime(1990, 10, 1)},
                                    new Employee {Id = 3, FirstName = "Betty", LastName = "Green", DoJ = new DateTime(1990, 10, 1)},
                                    new Employee {Id = 4, FirstName = "Mike", LastName = "Pagel", DoJ = new DateTime(1990, 10, 1)}
                                }.ToArray();
        employees.InsertBatch(employeesToAdd);
        Assert.IsTrue(employees.FindAllAs<Employee>().Any(x => x.FirstName == "Betty"));
    }           
}

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. Read more..

Creating a Node.js program in Cloud9 IDE

Cloud9 IDE is an online development environment for Javascript and Node.js applications as well as HTML, CSS, PHP, Java, Ruby and 23 other languages. Cloud9 IDE lets you build, debug, and run your Node.js applications within the browser. Set breakpoints, follow the call stack, and analyze your performance. In this post I’ll demonstrate how to build a simple Node.js server app using Cloud9 IDE. Read mode...