Calling NEO4J Rest API from node.js without any Library

As we can call and get results from REST API for any server, following is a test where we have called and got result from Neo4j REST Api using node.js. Here our call is a raw NEO4J Api call from node.js for a GET method which returns a particular node.  May be we will make a wrapper library in node,js for the Cypher query calls  with GET, POST methods for academic purposes, where we can really learn and measure the performance of the wrapper library.

Below is the code for the purpose and documented in-line –


var http = require('http'); // Required node http module to make the get method call

var optionsget = {
    host : 'localhost',            // Neo4j Server Host name
    port : 7474,                   // Neo4j Server port 
    path : '/db/data/node/1',      // specific query to the server
    method : 'GET'                 // method for the query
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

var reqGet = http.get(optionsget, function(res) {   // The get method call
    console.log("statusCode: ", res.statusCode);

    buffer='';

    res.on('data', function(d) {   // gathering data while receiving it in response data emit through 
        buffer += d.toString();    // callback
    });

    res.on('end', function() {     // On end of getting data from response callback, parsed it to JSON object        console.info('GET result:\n');
        jsonData = JSON.parse(buffer);
        console.log(jsonData.data);
        console.info('\n\nResult completed');
    });

});
reqGet.on('error', function(e) {
    console.error(e);
});

reqGet.end();  // End of the request

Following is the output of the above call –


Options prepared:
{ host: 'localhost',
  port: 7474,
  path: '/db/data/node/1',
  method: 'GET' }
Do the GET call
statusCode:  200
GET result:

{ released: 1999,
  title: 'The Matrix',
  tagline: 'Welcome to the Real World' }

Result completed

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

Enter your email address:

Delivered by FeedBurner

Leave a Reply

Your email address will not be published. Required fields are marked *