Implement Data Serialization using Node.js for JSON Data

Posted by: Mahesh Sabnis , on 12/5/2015, in Category Node.js
Views: 12822
Abstract: Node.js provides an easy to use mechanism to manage data serialization for the JavaScript object in the form of JSON

In modern web application development, data communication plays a very important role. Finally it’s all about data which is needed by the client application from the server. The hosting environment for the web application need to take proactive actions to make sure that the requested data must be serialized as per web standards. The ability of the data serialization in cross-application and cross-network scenarios in an open format holds utmost importance in web applications and the web host has to take this responsibility.

 

Since Node.js is based on JavaScript, we can make use of JavaScript Object Notation (JSON) and XML message format for the data communication. JSON being more close to JavaScript objects in its own way, we can easily implement data serialization from Node.js. In the application developed using Node.js, we need to decide from where we can read data and serialize it in the JSON and XML format.

Prerequisites for the implementation.

This application is developed using Visual Studio Code. This is a new FREE cross-platform editor for building and debugging modern web and cloud applications. We also need Node.js tools so that we can install necessary Node modules. Please visit following links to grab related information about Visual Studio Code.

https://code.visualstudio.com/Docs/editor/codebasics

https://code.visualstudio.com/Docs/runtimes/nodejs

Download Node tools from the following link https://github.com/Microsoft/nodejstools#readmejs

Implementing Node.js application

Step 1: Create a folder on your drive (e.g. E:\) with the name VSCodeDataSerialization. Under this folder, add a new folder of name Scripts. We will be using this to store our application files. Open Visual Studio Code. Open the VSCodeDataSerialization folder using File > Open Folder Option. Once the folder is opened, the option for creating new file will be displayed as shown in the following image. Select the Script folder and click on the new file icon as shown in the following image. This will provide a blank textbox where you create a new file name as appJSON.js.

data-serialization

Step 2: In appJSON.js file, we will write logic for converting JavaScript object to JSON using JSON.stringify () function. We will serialize this object over http. Add the following code in appJSON.js

//1.
var http = require('http');

//2. 
var ProductInfo = [
    { ProdId: 101, ProdName: "Laptop" },
    { ProdId: 102, ProdName: "Desktop" },
    { ProdId: 103, ProdName: "Tablet" },
    { ProdId: 104, ProdName: "Mobile" },
    { ProdId: 105, ProdName: "Router" },
    { ProdId: 106, ProdName: "Printer" }
];

var server = http.createServer(function (req, resp) {
    //3.
    resp.writeHead(200, { 'Content-Type': 'application-json' });
    //4.
    resp.end(JSON.stringify(ProductInfo));
});
//5.
server.listen(5050);

console.log('Server Started on 5050');

 

The above code performs the following operations. (Note: Comments on each line match with the numbers used in the following points.)

1. Loads the http module. Since we need to serialize data over http we need to create a web server using code.

2. Define JavaScript object which contains product information.

3. Write the response head with the Http Status code as 200 (ok) and the header content type as JSON.

4. Serialize the JavaScript object in the form of JSON using JSON.stringify () function by passing the JavaScript object to it.

5. Start accepting requests on the port 5050.

Step 3: Right-Click on the app.js and select Open in Command Prompt option. This will show the command prompt from where we can run the application.

command-prompt

Step 4: On the Node command prompt, add the following command

Node appJSON
json

This indicates a web server has started.

Step 5: Open any browser and enter the following address in the address bar:

http://localhost:5050

image

This shows the serialized JSON data. This data can be consumed in any client side application using JavaScript frameworks e.g. Angular OR JavaScript libraries e.g. jQuery using ajax() functions.

Note: If you are using Express.js 3x the response object has a json() method which  returns the response in JSON format.

Please visit the following link to know more about the client side consumption of JSON data exposed from the Node.js

http://www.dotnetcurry.com/aspnet-mvc/1135/using-nodejs-express-mongodb-meanstack-in-aspnet-mvc

In the above code have serialized the JavaScript object into JSON. We can make use of JSON.parse () function to store the JSON data into the JavaScript object.

 

Conclusion

Node.Js provides an easy to use mechanism to manage the data serialization for the JavaScript object in the form of JSON. This leads to a lightweight data communication from Node.js to the client applications.

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and Microsoft MVP having over 17 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook