Subscribe via RSS Feed

Calling Remote Rest API from Node.js to integrate PostGreSQL

July 9, 2013 0 Comments

NodeJS Remote REST API (1)

This Post is an odd requirement which evolved during our work. Actually we had started to explore the way to integrate Node.js and PostgreSql based in our Applications. After a little bit of googling, we found the node-postgres module on github. I tried to install the npm in mu windows 7 machine. Unfortunately the module was not installing in the machine due to some typical VCBuild load error.

Then to make the bridge between node.js and postgresql, I thought of another idea to make REST call with Java Jersey to PostGreSql and make the remote REST call from Node.js server. Now one question can arise – why another http request within a web request?

Actually we are integrating some archives of data in Neo4j, MongoDB and PostgreSql and we had selected node.js as a server to understand the application scaling. Also we hope node.js will have new modules of development and there will be much more work in node.js server. Now we had already integrated Neo4J and MongoDB with node.js. So here for PostGreSql also, node.js was the default choice for application server.

Now here we will describe the Node.js part and for the REST based backend, we have used the same one – which is in Developing Java REST web applications with Backbone.js.  We have used MySql for Database, which we can easily convert into PostgreSql specific code.

So here is the code in node.js with the commented sections -

var application_root = __dirname,
    express = require("express"), // Initialisation of Express.js module for Node.js REST Calling
    path = require("path");

var app = express();              // Express variable

var http = require('http');
var d = '';
// GET Request options configuration
var optionsget = {
    host : 'localhost',
    port : 8080,
    path : '/brandserver/rest/wines', // url with parameters
    method : 'GET' // GET Method

// Express Configuration
app.configure(function () {
    app.use(express.static(path.join(application_root, "public")));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

// Preparing Express REST API 
    app.get('/getRemoteData', function (req, res) {
        res.writeHead(200, {'Content-Type': 'application/json'});
        // HTTP GET request
        var reqGet = http.request(optionsget, function(response) {
            response.on('data', function(data) { 
                  res.end(data); // Writing the Remote REST Call response to the Express REST API "getRemoteData"

        reqGet.on('error', function(e) {
app.listen(1212); // Node.js server is running in port 1212.

Now, we will next unit test all our get and post APIs for the Remote REST calls and understand whether this solution will remain viable or not for production scale applications of our reach.

Also if you find this article helpful, you can connect us in Google+ and Twitter.

Enter your email address:

Delivered by FeedBurner