https.get response ondata callback fires multiple times

While working with Facebook graph api, I have used https.get to make a request for facebook user data.

var optionsP = {
        host: 'graph.facebook.com',     
        path: '/me?access_token=XXXX'
    };

    https.get(optionsP, function(resp) {                                        
        resp.on('data', function(d) {                                   
            console.log('ondata')
            console.log(d.length)
            process.stdout.write(d)
        }); 
    }).on('error', function(e) {
        console.error(e);
});

But the response data comes as 2 parts! First time it prints upto 1034 characters, then again same call back will work and prints remaining 1347 characters. What is the reason for these partial responses?

That's normal. resp is a stream. It's a ClientResponse object, that implements the readable stream interface. Here are the docs: http://nodejs.org/api/http.html#http_http_clientresponse

You can either pipe the output somewhere that accepts streams, or store it in a buffer until you receive the 'end' event.

Here is an example that stores the data in a String in memory, until it has all arrived:

https.get(optionsP, function(resp) {                                        
    resp.setEncoding(); //Now the data is a string!
    var store = "";
    resp.on('data', function(d) {
        store += d;
    }); 
    resp.on('end', function() {
        console.log("this is all: " + store);
    });
}).on('error', function(e) {
    console.error(e);
});