http - chunk data logging in node.js -


i have following function in node.js inside http.request()

       res.on('data', function (chunk) {     var sr="response: "+chunk;     console.log(chunk);     }); 

i in console

<buffer 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f  64 69 6e 67 3d 22 75 74 66 2d 38 22 20 3f 3e 3c 72 65 73 75 6c 74 3e 3c 73 75 63  ...> 

but when use this:

res.on('data', function (chunk) {     var sr="response: "+chunk;     console.log(sr);     }); 

i proper xml response this:

responose: .....xml responose..... 

i don't understand why need append string output proper response. , meant response generated in first code?

chunk buffer, node's way of storing binary data.

because it's binary data, need convert string before can show (otherwise, console.log show object representation). 1 method append string (your second example that), method call tostring() on it:

console.log(chunk.tostring()); 

however, think has potential of failing when chunk contains incomplete characters (an utf-8 character can consist of multiple bytes, don't guarantee chunk isn't cut off right in middle of such byte string).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -