Monday 18 June 2012

Node.js to ColdFusion - REST api


In my earlier post I explained how to expose a pdf service using ColdFusion REST api here.
In that post we saw that from the client side if I will pass a html document to this service it will convert it into pdf document and will return it back to the client. Also in my earlier post I gave an example to consume it using CF client itself. But there are times when your client side may not be ColdFusion. For that purpose I am giving an example to consume it using Node.js

Node.js server will act as a client over here and will contact ColdFusion to convert a document into pdf document.

Let's take a look at the code.


/*---code starts---*/
function handlepdf(data) {
 
var http = require('http');
var fs = require('fs');

var options = {
  host: CFip,
  port: CFport,
  path: '/rest/services/pdf',
  method: 'POST',
  headers: {
   'Accept': 'text/xml'
  }
 };
 
 var req = http.request(options, function(res) {
  var allchunk = "";

  res.setEncoding('utf8');
  
  res.on('data', function (chunk) {
   allchunk += chunk;
  });
  
  res.on('end', function () {
   var file = allchunk;
    
   fs.writeFile( 'sample.pdf', allchunk, 'base64', function(err) {
    if(err) {
     console.log(err);
     } else {
     console.log("The file was saved!");
    }
   });
  });
 });
 
 req.write(data);
 req.end();
}
/*---code ends--*/
..

Now lets understand the code:

I have written a function handlepdf(data), which accepts the html document as an argument. You can call this function as a callback handler for any event you like.

In this function I am making a http call to ColdFusion server because finally http request is all required for REST to work. I am setting the options of this request to match the CFC I created in my previous blog here

  1. method - post
  2. ip - CF server ip
  3. port - CF server port
  4. path - rest/services/pdf
  5. accept header - "text/xml"
Now moving ahead there is another function which will be called when this http.request will be ready.
In this function I am initializing a variable "allchunk", this is the variable where our complete pdf will be saved.
And on res.on('data'), I have called another function. This function will be called whenever data will be received on response of this request. Inside this function I am appending all the data to 'allchunk'.

One more way to do this was I could have taken allchunk in res.on('data') function itself but the limitation with that approach is that sometimes if the file is big then the response comes in small chunks several times. So using the above used approach will give you complete file.

Once I recieved complete file then on res.on('end'), I am writing this file from server itself, you can however pass to any client. For writing this file I am using filesystem package and writing it with encoding base64. Please take care of the encodings used while calling http request and saving the file otherwise your file could be corrupt.

In the end I am writing the html data in request and closing it, which will call function we have discussed above.

And extremely sorry for incomplete code of node.js but I just wanted to focus on calling ColdFusion part.

11 comments:

  1. Thanks Admin, I just begin my career in sever side web application development for that I complete the Node JS training but I wish to explore more in that, truly your blog provide some useful information and I would like to share your blog in my friends circle so keep sharing.
    Regards,
    Node JS training in chennai

    ReplyDelete
  2. It is very useful information about Node Js. This is the place for learner and glad to be here in this blog Thank you
    Node Js Training in Hyderabad

    Best Node JsTraining in Hyderabad
    Node Js Online Training
    Best Node Js Training in india


    ReplyDelete
  3. Looking for the best node.js company? Here is the list of best NodeJS developers that offer excellent Node JS development services.

    ReplyDelete
  4. Great.. If you are searching for best Node js training institute then visit the website.

    ReplyDelete
  5. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles learn Node JS training

    ReplyDelete
  6. Best blog , informative one. Practical learing make your career in IT industry. Join practical based PHP classes in Nagpur with free job placement services.

    ReplyDelete
  7. Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. I really like this post and Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. I am also providing coaching on angular js Angular training in Hyderabad just go through the link
    angular js
    MindQ Systems provides Training on Angular js, Angular2, Angular4, Angular5, Angular6, Angular7, Angular8. Mind Q provides Classroom Training, Online Training and video course for Angular Training. Mindq provides Training with an expert Trainer. Enroll now and schedule a demo session with the trainer.

    ReplyDelete
  8. Awesome artical and briefly explain information.Thanks to sharing information.and we have also train the student for IT training course in surat

    ReplyDelete