I have the following Node.js code that calls a weather webservice to get a json repsonse:
var reqGet = https.request(optionsgetmsg, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
// console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result after POST:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});
return d;
});
When I use process.stdout.write(d) the output to the terminal is pretty JSON format text like below:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"geolookup": 1
,
"conditions": 1
}
}
, "location": {
"type":"CITY",
"country":"US",
"country_iso3166":"US",
"country_name":"USA",
"state":"IN",
"city":"Indianapolis",
"tz_short":"EDT",
"tz_long":"America/Indianapolis"
}
}
However when I try to emit d using socket.io, it turns into a bunch of numbers when viewing the object in chrome dev tools.
io.sockets.on('connection',function(socketWeather){
socketWeather.emit('weather', { weather: d });
});
Chrome console output (a huge array containing 8616 random numbers):
Object {weather: Array[8616]}
How can I get the pretty JSON formatted text pushed correctly to my client?
UPDATE: I just noticed that while process.stdout.write(d) gives me nice JSON, console.info(d) and console.log(d) both print out this in the terminal:
<Buffer 0a 7b 0a 20 20 22 72 65 73 70 6f 6e 73 65 22 3a 20
7b 0a 20 20 22 76 65 72 73 69 6f 6e 22 3a 22 30 2e 31 22
2c 0a 20 20 22 74 65 72 6d 73 6f 66 53 65 72 ...>