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.