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.on('data', function (chunk) {
   allchunk += chunk;
  res.on('end', function () {
   var file = allchunk;
   fs.writeFile( 'sample.pdf', allchunk, 'base64', function(err) {
    if(err) {
     } else {
     console.log("The file was saved!");
/*---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.

1 comment:

  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.
    Node JS training in chennai