1

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 ...>
2
  • 1
    Have you tried wrapping the object you are emitting in JSON.stringify()? I think you will need to do that -- then on the client use JSON.parse() to convert back to an object. Commented May 9, 2014 at 19:04
  • @Catalyst It didn't work. Using both JSON methods still gave me numbers, but I did notice something interesting (see my update). Commented May 9, 2014 at 22:06

1 Answer 1

2

The problem that you're having is that the data is being returned from a stream. stdout supports streams so it appears the way it should. console.log on the other hand attaches a newline after every instance so its breaking the stream before it pipes it to stdout so the buffer gets written directly.

Instead of logging on each data even build up the data into a variable and handle the output during the end event.

  var response = '';
  res.on('data', function(d) {
    response += data;
  });
  res.on('end', function(d) {
    console.log(response);
    // now you can do what you need to with it including passing it to the socket
  });

As an alternative you could handle it on the browser side by turning the stream buffer into a string once it's arrived but personally I'd rather keep that logic on the backend.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked great. I did have to change your code to this: response += d; . I also used data = JSON.parse(data.weather); on the client side to get the object how I wanted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.