The data
event is fired multiple times with 'chunks' of the body as they are downloaded and an end
event when all chunks have been downloaded.
With Node supporting Promises now, I created a simple wrapper to return the concatenated chunks through a Promise:
const httpGet = url => { return new Promise((resolve, reject) => { http.get(url, res => { res.setEncoding('utf8'); let body = ''; res.on('data', chunk => body += chunk); res.on('end', () => resolve(body)); }).on('error', reject); });};
You can call it from an async function with:
const body = await httpGet('http://www.somesite.com');