I was doing some basic JSON parsing and wondered why node js collapses the objects when it is logged together with string
so for example in the code below, if I go console.log(processedData) it won't collapse the object and show the whole string but if I go console.log('Body: ' + processedData) it collapses the objects and goes [object Object][object Object].... I know how to expand them again using util but I was curious on the logic behind it as I am quite new to node. I think I might be missing out on something.
const http = require('http');
const util = require('util');
var options = {
host: 'http://nodejs.org/dist/index.json'
// host: 'en.wikipedia.org',
// path: '/w/api.php?action=query&list=allpages&apfrom=Imperial&aplimit=500&format=json'
};
var req = http.get('http://nodejs.org/dist/index.json', function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
let body = '';
res.on('data', function(chunk) {
body += chunk;
}).on('end', function() {
let processedData = JSON.parse(body);
console.log('Body : ' + processedData);
console.log(typeof body);
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});