Nodejs – Web’s new weapon!

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.

This is new thing in town and will be finest armor in your armory. It is easy to learn as it is almost same as Javascript and Python. There is a sample video tutorial on Nodejs site, which tells how easy it is to create a chat server. Once you will learn I can bet you will feel like you can do anything now.

There are several APIs based on it like Pusher, Platform as a Service Nodester and you will find many more things upon Google it. I will come with some good tutorial for it in some time later. Explore it and let me know if you find something interesting based on it.

Javascript Prototype

Javascript prototype is very useful thing. Say, we want to perform some sort of transformation or something else on string  like we want to find some pattern matching for validation. Example below is based on this.

Requires: Javascript

Using:

<script type="text/javascript">
String.prototype.found = null;        
var str = new String('my string');
function findpat(str)
{           
   var gotcha = str.search(/str/i);
   if(gotcha > 0 )
   {
       return true;
   }
   else
   {
       return false;
   }
}
var ret = findpat(str);
str.found = ret;
alert(str.found);
Explanation:

Line 2: We created a prototype object for string named found and equate it to null.
Line 3: We create str as String.
Line 7: Then in the code below it we find the pattern ‘str’ insensitive of case.
Line 17: Then we gave value to ‘found’ which can be used further.

Note:
  • Line 3 is important here as prototype for String is given, so str = ‘my string’ will not work. We have to declare it as String type. Same is to be done if it is being used with other type or custom types.

JSON – Javascript Object Notation

There were days when we used to create and use XML for transporting and store data and it was good at it. But I consider there was a problem with it getting data from it, I feel nausea. I used ‘was’ here because I also consider it as the way our daddies used to transfer data. Now is the day of JSON.

JSON is cool, easy, requires less memory(though we are not bothered). You can use it with any language and I love it. Frankly, I gave up XML before learning it because of JSON.

JSON looks like array we make in C\C++ or any other language. As we are familiar with the array, it makes understanding of JSON easy. It looks like :

{'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } };

Let’s use it with PHP and Javascript.

PHP:

$array = new array('data'=>array('J','S','O','N'), 'this'=>'json');
$json = json_encode($array);
$json = json_decode($json);
echo $json->data[0];
//output: J
echo $json->this;
//output: json

Javascript:

var json = {'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } };
alert(json['data'][0]);
//output: J
alert(json->data[0])
//output: J
alert(json->this);
//output: json
alert(json['this'])
//output: json

We have studying here for PHP and Javscript as it is used mostly for web development. Go to json.org for better understanding and using with other languages.