Saturday, February 1, 2014

Using Node.js to build a site that returns JSON data


I am working on a project that integrates with multiple other vendors using Http endpoint. We send http requests with JSON payload and the vendors has to consume that and respond with a response with JSON payload. I wanted to have a simple dummy site that takes the request and responds with the same kind of data I expect from the vendors. The whole objective of having this is to mock the vendor and test the integration. Moreover, I wanted an easy and fast process to create, deploy and start multiple mock vendor sites.

Node.js can be used to achieve this. For those of you who are already familiar with javascript, the code needed to create and run these sites is very minimal.


Note: this post is not supposed to be a tutorial on Node.js. That would be a topic for another day.Once I am comfortable enough with the nitty gritty details of Node.js, I hope to come back with a post.

I am using a Windows machine and here are the steps I followed,

1. Install Node.js from here

  •  Node.js will install a console window that you can use as a     playground or IDE.  Here you can write and run any JavaScript code
  •  Node.js root directory is included in the PATH variable of  system environment  variables . Thus you can invoke the node command from your command prompt
 2. I created a folder c:\projects\nodejsfiles where I keep all my            Node.js related files.     
    Note: You can keep your node file at any directory you want.

3. Copy the code shown below(after step 5) into notepad and save        it as myhttpendpoint.js under  the directory I created above.
   
4.  Open Command Prompt and navigate to the path where the              myhttpendpoint.js is located.
    
5.  In my case  c:\projects\nodejsfiles and then run the command
     node myhttpendpoint.js

6. Voila! the server should be up and running now. If you hit the url http://127.0.0.1:1337 from browser, you     should    see the JSON response being returned.

        The code looks like the following,

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/json'});
  var responseObj = {
  id: null,
  detail: [
    {
      part1: [
        {
          property1: 55,
          property2: "hello there!",
          property3: 12.0,
          property4: null,
          property5: 0,
          property6: true
        }
      ],
    
    }
  ],
 
};
  res.end(JSON.stringify(responseObj));
  }).listen(1330, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1330/');



Few words on the code above,

  • Require(‘http’) will load the http module before the following code dependent on the module is executed.
  • The response content type is set to be application/json. The IANA registered content type for json is application/json. http://www.iana.org/assignments/media-types/media-types.xhtml#application 
  •  JSON.stringfy is used to change the response JavaScript object into a readable JSON string format.
  • The server is set to listen to incoming requests at port 1330.







No comments:

Post a Comment