Category Archives: General

Essential Node.js Modules for Web Application Development

In this blog post I have tried to explain essential node.js modules which may help you to make your web application robust and more users friendly.

1. Express Framework – Go to Website

Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multiple pages, and hybrid web applications. It is complete package for your framework related requirements. Visit this website to know how it works and who uses express framework.

Install via npm: $ npm install –g express

2. Jade – Go to Website

Jade is a high performance template engine heavily influenced by Haml and implemented with JavaScript for node. Based on my experience it’s very easy to use and understand.  There are many instances while development, you will need HTML to Jade converter, so here it is for you.

Install via npm: $ npm install jade

3. My-Sql – Go to Website

My-sql module is driver for node.js. You can interact with any MySQL database, run any query, stored procedure. There are lot of configuration options available in this module which you can set depending upon your application needs. Interestingly this module also provide connection pooling mechanism which gives this module added advantage over other my-sql modules for node.js.

Install via npm: $ npm install mysql@2.0.0-alpha9

4. Mongoose – Go to Website

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Install via npm: $ npm install mongoose

5. Session

Both express and connect has session management feature. If you want to learn how you can manage user’s session using express.js then read this interesting blog post.

Security is measure concern when working with any of the modules in this area. I don’t find any explanation on session replay attack and session hijacking attack prevention in express.js. So according to my experience you have to write your own logic to prevent session replay and session hijacking attack.

Also I would recommend you to read this blog post if you are really concerned about your website or web application security.

6. Nodemon – Go to Website

Nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application. For use during development of a node.js based application. It’s a wrapper around node. Many times we keep changing our server side javascript files but we forget to restart node server, there this module helps us to automate this process.

Install via npm: $ npm install nodemon

7. Nodemailer – Go to Website

Nodemailer is an easy to use module to send e-mails with Node.JS (using SMTP or sendmail or Amazon SES) and is unicode friendly – You can use any characters you like.  Not only text message it also support large files as an attachment.

Install via npm: $ npm install nodemailer

8. – Go to Website

Socket.IO aims to make real time apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It’s care-free real time 100% in JavaScript. Real time dashboard, stock market applications are the best candidate where we can use this module. If you want to keep your users updated without refreshing page then there is no better option available then this module.

Install via npm: $ npm install

9. Async – Go to Website

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Originally it was designed for node.js but now you have option to use it on client side js if you wish to. If you are working with event driven programming language then module like this will be very handy and can save your lot of time.

Install via npm: $ npm install async

If you want to see practical examples for async module then I would suggest you read this blog post.

10. Nodefly – Go to Website

Nodefly is application monitoring tool. You can monitor you application performance from anywhere in the world. When application go live you will always need some tool which continuously monitor your application performance and give you an alert whenever anything bad happens. Nodefly is very powerful module but it has only one requirement, internet connection on server. Nodefly is third party service which will collect performance metrics and sync it on their server.

Install via npm: $ npm install nodefly

11. Uglify – Go to GitHub 

Uglify helps you to make your web page lighter by making all JavaScript files minified on the fly. This means that developer will always work on non-compressed javascript version but when the same file reach to client (Brower) it will be loaded minified. This is the benefit of using node.js.  In today’s world loading web page faster is become essential otherwise no one would have time and care to visit your web page again.

Install via npm: $ npm install uglify-js@1

Flat File Database

Few days back, I was searching for database to store data. It may be big, small or medium size data. Operation which I was supposed to do was Select, Where and Aggregation nothing fancy. I started with mongoDB then I tried MySQL and lastly I ended with Flat CSV file. In this blog post, I have written my experience using all of the above systems.

I have inserted 34 Million+ rows in mongoDB, MySQL and Flat CSV file. Below are the sizes different system has created on disk.

mongoDB: 26 GB
mySQL: 13.37 GB
CSV File: 14.42 GB

My system’s configuration is
Processor: Intel i3 quad core

Insertion Speed
Record insertion speed in mongoDB is far better than MySQL. It’s almost 3-4 times faster than MySQL. Inserting record in CSV file has identical result as mongoDB. The reason is both are document oriented database.

I had done some simple select query with single field where condition in MySQL and mongoDB but result was very frustrating. After waiting for almost 20 minutes neither MySQL nor mongoDB has given any output. Then I had created index in MySQL but still result is identical. Even creating an index on such big table itself is big headache. It will take long to finish. In case of mongoDB it took almost 1.30 hour just to create an index and even after creating index mongoDB also had not be able to answer my query. Query was simple select with some where condition. You won’t believe guys I had tuned MySQL for optimal performance as recommended by MySQL itself. And also my table is MyISAM which is best engine for data retrieval.

After simple select query I tried to fire some aggregation queries on MySQL and mongoDB database. Query which I had fired is simple group by on single column and sum of single column with some where condition on single field.

Select Plnt , Sum(Volumn) as Vol
From tbl_bigData
Where Region in (‘Gujarat’,’Delhi’,’Mumbai’)
Group by(Plnt);

This query is pretty straightforward and I was expecting result this time but despite of giving result, query was keep running. Another frustrating moment for me.

I have done all sort of tuning, indexing on my database in MySQL but I never achieved any output.

Then I have read somewhere about MapReduce in MongoDB. Firstly, I have created index of plnt, region column and then I submitted map and reduce function to mongoDB and then I executed MapReduce function in MongoDB. After waiting for 15 minute when I show console process was finished only 8% so without wasting time I stopped query execution.

Flat CSV File (Happy Moments Start)
After experimenting all of the above methods and tactics I got undesirable results, so I consulted one of my team members. He has very good knowledge dealing with flat file as database.

We had created method where we have to pass n-number of filter conditions (where conditions), n-number of aggregation parameters and resulting column index, because in CSV everything is based on index.

So using CSV file read not only I got desirable result but also good performance. Using single file read I am able to filter records and aggregate data all together. Interesting part is that you can return n-number of result set using single file read.

For example:
Result 1:
Filter : Region IN (‘Gujarat’,’Delhi’,’Mumbai’)
Select: Region, Plnt, Volumn

Result 2:
Filter : Region IN (‘Gujarat’,’Delhi’,’Mumbai’)
Aggregate: Sum(Plnt)
Select: Plnt, Sum(Plnt)

Yes, there are few limitations associated with flat file as database. If these limitations are unavoidable for you then this approach is not for you. According to me this approach is best use when you have big data say 100 GB+ data and you want some analysis on the that data.

Please correct me if I was wrong somewhere in this blog post.

Geographical SVG Map With Example

In web if you want to provide any geographical svg map related solution then consider map as some random shape. Every map is nothing but a random shape. And in web if you want to draw any shape SVG is the best option. SVG stands for Scalable Vector Graphics.

But to draw shape like this will require lot of skill and time. So, I find one website which gives us almost all countries detailed map in different formats like GIF, PDF, SVG and many other.

Please visit

I have taken India map from This is SVG map.

Rest is depending upon your logic. :)

Apply Culture In Silverlight Application

Culture In Silverlight Application plays a very important role. Silverlight application always sits on client browser and works as a plug-in. So you always need to keep in mind that control panel setting of different user might be different especially regional and language settings. Different user on different countries uses different date, time and number formats so while building Silverlight application you have to consider this fact and build your application accordingly and you need to make sure that you have applied culture in Silverlight application.

Recently I come across issue where application uses M/d/yy format date throughout application and it worked fine till user had not changed anything from regional settings. When user changed short date time format from control panel > regional and language settings application started giving errors of bad date and time. So I needed to apply default en-US culture in my application to resolve this issue. What I have done is I have applied en-US culture in current thread on application startup i.e. app.xaml.cs.

Here is the code for the same.

private void Application_Startup(object sender, StartupEventArgs e)
System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-US");
c.DateTimeFormat.LongDatePattern = "dddd, MMMM dd, yyyy";
c.DateTimeFormat.ShortDatePattern = "M\\/d\\/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = c;
System.Threading.Thread.CurrentThread.CurrentUICulture = c;

I hope this will help others to solve the same problem.

JSON in Javascript Explained

What is JSON in Javascript?

JSON stands for Javascript Object Notation. JSON is nothing but an object which contains key value pairs. Advantage of JSON over other data format is it is lightweight. JSON is platform independent as well as language independent data exchange format.

JSON object example


var jsonObj = {id:1,name:”hiteshagja”};

In this example Keys are id and name and Values are 1(typeof int), hiteshagja(typeof string).

So If you write alert( in javascript you will get alert containing text hiteshagja. Isn’t this so amazing about JSON.

Add New Property(key) to already existing JSON object.

Example 1:””;


jsonObj[“email”]= ””;

This will add new key email to already existing jsonObj. You don’t need to write anything else other than this.

But if you write like

Example 2:””;



This will only update our JSON object key value from hiteshagja to

JSON currently supports

Data types and
Null value

Get JSON object’s keys count


will return number of keys in given JSON object. In our case this will return 3(id,name and email).

Delete key

delete jsonObj[“email”]

this will remove email key value pair from jsonObj. Delete is a keyword which will accomplish this task.

String to JSON object


var jsonStr=’{“id”:”1”,”name”,”hiteshagja”}’;

Let’s say you have string value just like shown above and you want to convert it into JSON object.




var jsonObj=eval(jsonStr);

will convert JSON string into object.

JSON object to String

In some cases you will be needed converting JSON object into string there you can use conversion like shown below.


var jsonStr= JSON.stringify(jsonObj);

This will convert jsonObj into string.

JSON Array

Till now you have seen how to create single JSON object. Now in some cases we will need to store data as an array. Let’s see how we can do so.

Example 1:

var jsonObj={id:1,name:”hiteshagja”,phone:[1111111111,2222222222]};

Here as you can see I have written phone value as an array.

You can access phone number using[0] just like you are accessing other array objects.

Example 2:

var jsonObj={

Products:[ {name:”Product1”










Example 2 is real time scenario where you will have array of products. jsonObj is array of products which contains 3 products in our example.

If you want to access product 2 then you can simple write


I will keep updating this post whenever I will find anything interesting about JSON.

Silverlight – Listbox Scrollbar Problem

Recently I have observed that listbox scrollbar has virtualization problem when we re-bind listbox. Let me explain this with an example.

I have added 50 items into listbox control programmatically. Here is the screenshot for the same.

Image after 50 Items bound to listbox

Normal Listbox Binding

Re-bind listbox

Re-bind button click without scroll bar move

As you can see I have added two buttons to Reset and to Re-bind listbox again.

If I click on any of the button everything will work as expected but real problem is when we move scroll bar to bottom of the listbox and we re-bind listbox data.

See what kind of problem you shall face.

Problem displaying data

Solution :

Use scrolltoview function after assigning ItemsSource to listbox. See code snippet below.

[sourcecode language="csharp"] List<ListBoxItem> a=GetItemsList(txt,count);

listbox1.ItemsSource = a;







Dealing with DateTime Formats in C#

Dealing with datetime formats in C# is very easy if you use framework functions properly. Recently I come across very ugly error which makes my application crashed. Actually it was problem converting DateTime for specific format. You should make your DateTime conversion independent of client computer’s culture setting. This thought in mind I am going to present this post.

1.       Convert String  to DateTime

Let’s take an example:

Suppose you have

[sourcecode language="csharp"] String strDate = "10/23/2011"; [/sourcecode]

Which is in MM/dd/yyyy format and you want to convert it in DateTime variable then you should write like

[sourcecode language="csharp"] DateTime dt = Convert.ToDateTime(strDate,System.Globalization.CultureInfo.InvariantCulture); [/sourcecode]

You could write above code if you are sure that the format is in MM/dd/yyyy. If the format is in say dd/MM/yyyy then DatTime.ParseExact is the perfect option for you.

Code above can be rewritten as

[sourcecode language="csharp"] DateTime dt = DateTime.ParseExact(strDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); [/sourcecode]

In all above code lines I have mentioned System.Globalization.CultureInfo.InvariantCulture parameter, which will make date format, culture independent.

2.       Convert Datetime to Specific Format

Let’s say you have

[sourcecode language="csharp"] DateTime dt = DateTime.Now; [/sourcecode]

and you want to convert it in different format. 

Example : Format : dd-mmm-yyyy

[sourcecode language="csharp"] String s = dt.ToString("dd/MMM/yyyy"); [/sourcecode]

But this will be overridden by current culture.

So you should do something like this

[sourcecode language="csharp"] String s = dt.ToString("dd/MMM/yyyy", System.Globalization.CultureInfo.InvariantCulture); [/sourcecode]

In short, CultureInfo parameter plays an important role.

Run Executable (exe/bat) file from Web Page

To run an exe from your web page is quite easy process. I have used simple JavaScript to accomplish this task. Have a look at the code listed below.

[code lang="javascript"]
function WriteToFile() {

var fso, s,folderName;

fso = new ActiveXObject("Scripting.FileSystemObject");

if (!fso.FolderExists("C:\HITESH"))


folderName=fso.CreateFolder("C:\ HITESH ");


if (!fso.FileExists("C:\ HITESH \LaunchApp.bat")){

s = fso.OpenTextFile("C:\ HITESH \LaunchApp.bat" , 8, 1, -2);

s.writeline("IF EXIST d:\hitesh GOTO LABEL1”);

s.writeline("IF EXIST k:\hitesh GOTO LABEL2”);


s.writeline("D: ”);




s.writeline("K: ”);






function LaunchApp() {


var oShell = new ActiveXObject("WScript.Shell");

var prog = "C:\ HITESH \LaunchApp.bat"; ('"'+prog+'"',1);


This javascript will create an object of WScript.Shell and will execute DOS based batch file. In addition with this I have also created LaunchApp.bat file programmatically if it does not exist on the client side (So you don’t need to worry about whether file exist or not J). This file will execute EXE depending upon conditions specified in the LaunchApp.bat. LaunchApp() is the starting point.

Populate data in Gridview using jQuery AJAX

I search a lot in Google for a whole day but didn’t find any perfect example to populate data in gridview using jquery ajax. Then I have decided to do it myself and I find it easy at the end of the day.

In the example which I illustrated below follows the flow:

  1. On document ready method it initiates jQuery AJAX call.
  2. Response will be redirected to the ASPX page and in tern server will respond with JSON data.
  3. jQuery AJAX call back success method used to generates HTML Table with data.
  4. Add HTML to DIV.

Here is the code:


[code lang="html"]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script src="js/commons.js" type="text/javascript"></script>



<form id="form1" runat="server">

<div id="divperson">

<strong>[Newly generated HTML will seat here]</strong>






[sourcecode language="csharp"]
public partial class _Default : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)


if (!IsPostBack)


if (Request.QueryString["id"] == "person")




public void GetString()


JavaScriptSerializer _serializers = new JavaScriptSerializer();

string _personJSON = _serializers.Serialize(Person.GetPersonDetails());






<strong>//Person Class and Members</strong>

public class Person


public string Name { get; set; }

public string State { get; set; }

public string PhoneNumber { get; set; }

public Person(string name, string state, string phone)


Name = name;

State = state;

PhoneNumber = phone;


public static List<Person> GetPersonDetails()


List<Person> _person = new List<Person>();

_person.Add(new Person("Hitesh", "Gujarat", "123"));

_person.Add(new Person("Urvish", "Maharastra", "345"));

_person.Add(new Person("Deepak", "Madhya Pradesh", "456"));

return _person;



I just created Person class, created some properties and a static method to generate list of Person class objects. You need to include System.Collections.Generic and System.Web.Script.Serialization name space for List Class and JavaScriptSerializer class respectively.

In Page_Load method, based on the querystring value the GetString() method will call and JSON response will be generated and submitted as response. You need to catch this response on client side to retrive values within JSON formatted data.

commons.js (a separate JS file)

[code lang="javascript"]


type: "GET",


url: "Default.aspx",

dataType: "json",


var person;

var _html='<table border="1">';

_html += '<tr>';

_html += '<th>Name</th>';

_html += '<th>State</th>';

_html += '<th>Phone</th>';

_html += '</tr>';

for(index in resultdata)


_html +='<tr>' ;

_html +='<td>' + resultdata[index].Name + '</td>';

_html +='<td>' + resultdata[index].State + '</td>';

_html +='<td>' + resultdata[index].PhoneNumber + '</td>';

_html +='</tr>' ;


_html +='</table>' ;





Nothing fancy, using jquery.ajax method I made a GET request to Default.aspx page with some data. Page has return JSON data which I parsed in jquery.ajax success method.

Before you attempt to run this code in your app, make sure you have included jquery-1.4.2.min.js file.

Encode Text Message to PDU

Encode Text Message to PDU before submitting it to network. [Note: this is applicable if you are going to send SMS in PDU mode]. In this post I am going to explain how you can do this, but before that please download sample code from Planet Source code. This code contains Convert, Sending and Receiving modules. Frankly speaking I have got encoding/decoding logic from this code and it works perfectly for me. Only thing missing in this code is Padding Bit logic. Code is quite simpler and easily understandable. Please go through it and tell me if you have problem encoding text message into PDU.

How to add Padding bit for multipart SMS PDU?
Open the code > go to Convert Module > Find “CharHex”  Method.
Do replace this function with the code shown below.

Public Function CharHex(ByVal Txt As String, ByVal bit As Integer, ByVal PaddingBit As Boolean)
Dim i As Integer, bin As String, nbin As String, n As String
Dim bil As Integer, sisa As Integer, lbin As Integer, nol As String
bin = ""
nbin = ""
If bit = 7 Then
For i = 1 To Len(Txt) Step 2
n = Mid(Txt, i, 2)
bin = HexToBin(n) & bin
bil = Len(bin) bit
sisa = Len(bin) Mod bit
For i = 1 To (Len(bin) - sisa) Step bit
' MsgBox Chr$(HexToDec(BinToHex(Mid(bin, i + Sisa, bit))))
nbin = Chr$(HexToDec(BinToHex(Mid(bin, i + sisa, bit)))) & nbin
For i = 1 To Len(Txt)
n = Mid(Txt, i, 1)
bin = Biner(Asc(n)) & bin
If PaddingBit = True Then
bin = bin + "0"
End If
sisa = Len(bin) Mod bit
If sisa > 0 Then
For i = 1 To bit - sisa
nol = nol & "0"
End If
bin = nol & bin
bil = Len(bin) bit
For i = 1 To bil
nbin = nbin & BinToHex(Mid(bin, Len(bin) + 1 - bit * i, bit))
End If
CharHex = nbin
End Function

That’s it !!
Do let me know if you have any problem encoding text to PDU.


I am getting lot of queries regarding Message PDU for single message and concatenated message. So What I have done is I have created a quick sample to generate PDU for single message and for multipart SMS.

Download Sample (Generate SMS PDU)