To support the full range of possible HTTP applications, Node.js HTTP APIs are very low-level. Thus, the data does not get a piece by piece in general.
There are two approaches to this problem:
1) Collect data from several βdataβ events and add results
together before printing the output. Use the "end" event to determine when the thread is finished, and you can write the output.
var http = require('http') ; http.get('some/url' , function (resp) { var respContent = '' ; resp.on('data' , function (data) { respContent += data.toString() ;
2) Use a third-party package to abstract the difficulties associated with the use of collecting the entire data stream. Two different packages provide a useful API to solve this problem (most likely!): Bl (Buffer
List) and concat-stream; choose!
var http = require('http') ; var bl = require('bl') ; http.get('some/url', function (response) { response.pipe(bl(function (err, data) { if (err) { return console.error(err) } data = data.toString() ; console.log(data) ; })) }).on('error' , console.error) ;
Salar Nov 23 '16 at 13:33 2016-11-23 13:33
source share