Home > database >  What actually response.on('data', function(data) {}); do?
What actually response.on('data', function(data) {}); do?

Time:10-17

I want to understand what is happening under the hood on response.on("data").

When I request a web server, I get a response. And then I somehow use response.on("data" ...); and receive some kind of data. I want to know what data the program is taking.

For me it is like:

response {
  data {
    temperature: 50,
    city: "New York"
  }
  statusCode: 200
}

But that's not true, because then we could do just this: response.data and then take information from it.

CodePudding user response:

This "data" thing is a Readable Stream related event. It is not a JSON or anything of that sort. Put differently, when reading

response.on('data', function(data) {})

You should not fool yourself by thinking that the two mentions of "data" above are the same entities. They are related by design, but are two independent objects.

  • Related