Since I can't comment on the answers for this question I would like some help in here.
I have the exact same code but the output goes like this:
/**
* Created by Ramiro on 09/06/2015.
*/
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
//console.log(dbHandler.GetDatabase());
var req = http.request('http://127.0.0.1:5984/_myUri', function(res) {
res.setEncoding('utf8');
var body = '';
// Streams2 API
res.on('readable', function () {
var chunk = this.read() || '';
body += chunk;
console.log('chunk: ' + Buffer.byteLength(chunk) + ' bytes');
});
res.on('end', function () {
console.log('body: ' + Buffer.byteLength(body) + ' bytes');
});
req.on('error', function(e) {
console.log("error" + e.message);
});
});
req.end();
response.end();
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(3000);
With this output:
chunk: 230 bytes
chunk: 0 bytes
body: 230 bytes
chunk: 230 bytes
chunk: 0 bytes
body: 230 bytes
chunk: 230 bytes
chunk: 0 bytes
body: 230 bytes
which leads me to infer that:
- res.on 'readable' is being called twice:
- The whole callback is being called three times. Need your suggestions.
The 'readable' event is being thrown for each chunk that comes through, which sometimes will include a stray 0. I believe this is happening in your case, and since your console.log line is getting the aggregated "body" value and not the individual "chunk", you're seeing 230 both times. (It's returning 230 and then 0, but the total each time is 230). To get the total data, you'll just need to concatenate all the chunks that are called in the 'end' callback. See where this gets you:
This doesn't explain the 3 calls though. (When I run your exact code in mine, I get multiple 'chunk' lines for the reasons I explained above, but I only get one total set, and only one 'body' line.) Is this part of a larger test program? If so, maybe it's being called 3 times within some other loop? Or it may be leading to a page that is awkwardly refreshing or redirecting a few times and sending headers that aren't signaling an 'end'. I just don't know enough about how this function works in NodeJS to account for anomalies like that.