Simple HTTP GET with Node.JS

Problem

  • Need a simple HTTP GET call with return value

Solution

var http = require(“http”);
hget(“127.0.0.1”, “8086”,”/exampleCall”,
    function (data) {
        //process your data here
    });
function hget(url, port, path, callbackData) {
    var options = {
        host: url,
        port: port,
        path: path
    };
    callback = function (response) {
        var body = ”;
        response.on(‘data’, function (chunk) {
            body += chunk;
        });
        response.on(‘end’, function () {
            callbackData(body);
        });
    }
    var req = http.request(options, callback).end();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s