Wednesday, December 26, 2012

Simple way to remove methods associated with object

Here, I am going to extract object’s attributes(attributes only not method) from another object. In some case, I need attributes from object instead their methods. Though there is a way of use “for” loop, I used JSON. Also this will help us to clone object attributes.


Output In Firebug:

 




Note: Please write your queries and suggestion as comment.

Wednesday, August 22, 2012

NodeJs - Extract Keys from Memcache Server

Let's see how to extract keys from memcache server. And I have done a modification in Arnout Kazemier’s simple_cachedump.js and then improved the NodeJs responses by prior calculation of available number of keys.



Output:




Note: Please write your queries and suggestion as comment.

Sunday, July 15, 2012

PHP: How to kill global variables in php?

Here, I am going to show, How to de-scope or kill the global variable.  In below code, I have used to two global variable for my testing. And I have killed first  variable($killinscope) only within the function "scope" and the variable ($killinscope) will be available outside of the function. And then I have killed second variable ($killinuniverse) and the variable ($killinuniverse) wont available anywhere. To improve performance kill the global variable as soon as possible.



Output:



Note: Please write your queries and suggestion as comment.

Monday, July 9, 2012

NodeJs - Basic Server Implementation With MVC Architecture

Here, I come back with Nodejs and MVC concept. Yes, I want to use Nodejs modules more easy and flexible way. Before get into deep, just go through the following diagram.


Unlike PHP+Apache, In NodeJs, the code is live until the application terminate once application is started. So any careless code will leads to memory-leaks. So before start, we have to plan about structure of project. That is, there should be clear definition about place of modules and how they are going to communicate each other. So here, I have proposed a design with support MVC which is showed in diagram. There is three kind of modules, First one is core modules like http, fs and more. Second one is contributed modules like request, underscore and more. And third type is custom modules which are developed by application developer. I have to organize these three modules in right folder/directory. Luckily, Core modules are comes with NodeJs installation setup. And Contributed modules are placed in folder named with "node_modules" by NPM. So I have to take care only about custom modules.

Here, I feel keep those custom modules or files in separate folder without joining them as a part of contributed module. I called this separate folder as "Application" which is showed in diagram. Since I have control over my code, I divide them three part to support MVC and organize the code in files based on their responsibilities.

And more, as showed in diagram, I would like to keep the communication of those modules in application folder more strong and communication of modules between "Application" folder and "node_modules" folder is in comfortable level. I mean, On incoming request the modules in "Application" folder talks strongly within themselves. In another word, during the application start-up, the modules within "node_modules" folder interact each other and generate a base. The modules in "Application" folder are interact often, that means on every request. Since "Express" module has great control on handling routing, session and request body parsing, I am using "Express" to gain controller advantage. Overall, The system handles the request with respective to MVC.

Github: https://github.com/hilarudeens/NodeJs-BasicServer-MVC
Note: You can fork it at anytime. Help to improve the code.


Output: Folder view



Note: Please write your queries and suggestion as comment.


Wednesday, February 15, 2012

NodeJs : Create A Basic Html Server Using Simple File Read Operation

NodeJs : Create A Basic Html Server Using Simple File Read Operation

Here, I would like to implement a server which is read static html file and rendering other items like css, javascript and images to the client (browser you are using). To do this, I am going to use "Asynchronous File Read Operation". Because of my knowledge, I would believe NodeJs, only allow another task when it busy with file Read/Write operation. Here I’m going with this asynchronous operation to make NodeJs effective. To know more about NodeJs file system click here http://nodejs.org/docs/v0.3.1/api/fs.html .

And I want to my server also handle "File Not Found". Here I have showed my folder/directory structure in my xp system.



And following code has written in index.js located in the path "C:/…./htmlserver/folder".



And index.html looks like as follows,

And styles.css is


Here styles_1.css 



Why I'm writing the css in different file means just I want my server to handle multiple and different browser request. In the above index.html I have include nonexisting.js "script" tag because just I would like to setup my server to handle 404 error also. See following figure, there is no ".js" file in js folder.



There is a file page_404.html. I use this whenever invalid page request hit my server.



Output:






Note: Please write your queries and suggestion as comment.

Friday, February 3, 2012

NodeJs : REST Call Using http Library

Here I am showing how to bypass the incoming request to another site or how to do the operation like curl.

In the following code, your local Node server got incoming request via port 1337 and send it to "Google Maps API". And write the response (which is get from API) in your browser. 
Now write following code.

Output:



Note: Please write your queries and suggestion as comment.

Wednesday, February 1, 2012

NodeJs : Create and Install Simple Module using npm - Node Package Manager

Let’s see, how to create simple module and how to install the module using npm (Node Package Manager).

In NodeJs, module is a piece of code/function which is doing specific task or operation and modules are higher level of reusable code. Modules are expanding features of project and they are created in either in JavaScript or c++. 

Here I am going to show creation and installation of "Hello world" module. Let’s assume that we already created a project namely "helloworld_proj" and the project has a file namely "index.js" with following code.
 

Now I am going to create a module namely "helloworld_mod" to display "Hello World" in browser.

1. Create a folder named "helloworld_mod" in your project directory. Obviously you can keep your module anywhere in your system when it is in development stage.

2. And create a file "package.json"  and a folder named "lib" within the module folder.

3. Write following line of code in "package.json"  file.


Note: To know more about "package.json" click here http://docs.nodejitsu.com/articles/getting-started/npm/what-is-the-file-package-json.

4. And create a file named "index.js" within "lib" folder. Now the path of "index.js" file would be "C:\Program Files\nodejs\node_builds\helloworld_proj\helloworld_mod\lib\index.js".

5. And then write following code in index.js (located within "lib" folder).


6. Now your module ready to install. To install go to DOS-Command prompt and set base path to your project by using command >cd "C:\Program Files\nodejs\node_builds\helloworld_proj".

7. Now type the command >npm root. This gives path which is currently selected nodejs servers installation path. If it shows the path "C:\Program Files\nodejs\node_modules", that means NodeJs server install the module common for all project. Here assume that you want the "helloworld_mod" module only for "helloworld_proj".


Note : To know more about npm click here http://npmjs.org/doc/README.html

8. To install module within my "helloworld_proj", create a folder named "node_modules" within the project folder. Now the path would be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

9. Now again type the command >npm root and verify the installation path. It should be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

10. Now type the command >npm install helloworld_mod. This command install helloworld_mod and displays the detail "helloworld_mod@0.0.1 ./node_modules/helloworld_mod".



11. To confirm the installation, go to "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules" and you can see new folder with name "helloworld_mod" which is contain files and folders similar to your original module folder.

12. Now go to project folder "C:\Program Files\nodejs\node_builds\helloworld_proj"  and edit the file "index.js" as follows.


13. Start the server by type the command >node index.js . Now type the url http://127.0.0.1:1337/ in browser and press enter. Then the browser says "helloworld_mod says Hello World".  


For more details click here http://nodejs.org/docs/v0.4.4/api/modules.html.


Note: Please write your queries and suggestion as comment.


Wednesday, January 25, 2012

NodeJs: npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

If you are working in windows go through in following.

npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

It's occur either invalid JSON format or file created via Notepad by save as.
To avoid this, create a empty JSON file in DOS-Command Prompt (edit/copy con command) or here my simple way is,
1. Create a BAT file (.bat extension) and write the following command without double-quotes.
 ">package.json".
2. Copy the file to your module folder.
3. Run the BAT file. This will create a actual JSON file. Then write following line of code to make your NodeJs module installable.


Note: Please write your queries and suggestion as comment.

Tuesday, January 24, 2012

NodeJS : Get Starting With NodeJS on Windows Xp For Newbie

 NodeJS : Get Starting With NodeJS on Windows Xp For Newbie

1. Download Node server from http://nodejs.org/  



2. Once the installation is completed, the folder structure looks like as follows and installation path  in my system is "C:\Program Files\nodejs"






3. To write Your first "Hello World" program, create a folder namely "node_builds" within "C:\Program Files\nodejs". Let’s use this folder like "www in Wamp server" or "htdocs in Xampp Server". Of course,  NodeJs allow us to keep Your project anywhere in local system, always keep right folder structure would be better.



4. Now create a project folder namely "helloworld_proj". And then create a javascript file namely "index.js" within Your project folder and write following code in it. 


5. To run your "Hello World" program, go to DOS-command prompt and then set the base path of command prompt to your project folder by typing the command >cd "C:\Program Files\nodejs\node_builds\helloworld_proj". Now your command prompt would be



6. Now start node server by typing the command >node index.js . And then you gets log message which shows in above piece of code. 



7. Now your server is running. Then go to browser and type the url  "http://127.0.0.1:1337/" which is currently listening by our node server. Now your browser says "Hello World"


Note: Please leave your queries and suggestion as comment.

Monday, January 23, 2012

NodeJs: Convert xml to JSON using Sax Module

Sax is power full module that helps you to parse xml data. And my problem in this module is, I need to reconstruct xml data and produce JSON string which is include element name, attributes and its content. Of course, Sax parses the xml as two major parts, namely node and text. 
Here I have to join the text with node and produce final result as JSON string.
To do this, Here I have done simple array operation (push and pop) in Sax’s provided function onopentag and ontext.

Output:


Note: Please leave your queries and suggestion as comment.