Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBen Ridley
2,936 PointsWhat is a response object? Does the treehouse server respond with a javascript object, or does this work for all sites?
I was curious as to how we can call methods on the response object passed into our callback. How does this work? What even is the response object?
Thanks!
1 Answer
Steven Parker
231,275 PointsThe response object represents the outgoing data stream.
It contains many useful methods for sending data back to the client. Those methods are provided for you to use in creating your response to the request. Its properties and methods are described in detail in the node.js documentation.
JavaScript objects are not transmitted directly, but there are common methods for converting them to and from other forms. One very common form is JSON, which is used by Treehouse for sending student information.
Ben Ridley
2,936 PointsBen Ridley
2,936 PointsThanks Steven for a great answer. I checked out the node.js documentation, but I'm still curious about the description of the response object being 'created internally' by a http server. Is the response from a http request always an object because node is parsing the output stream into an object? Why isn't my response just a simple string, for example?
Thanks again!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsWe may have some terms confused here. I thought you were asking about a response object which would be used by a server to send a response to a request. But now it sounds like you are working from the client side. I think what you are calling a "response object" may actually be the IncomingMessage object that arrives in response to a request.
They are similar in some respects, and are both covered on that documentation page I linked. But on the client side, the IncomingMessage object represents the incoming data from the server. You'll likely attach an event listener to it to respond to the data event to get your actual message. I think in just another video or two from where you are in the course you will see this demonstrated.