I need to retrieve the body of a http.ServerResponse. Since it's a stream I assumed that I simply could add a response.on("data", function(){})
to retrieve the stream chunks. I put together a simple nodeJS http server.
const http = require('http');
http.createServer((request, response) => {
response.on("data", function(){
console.log("data",arguments)}
);
response.on("end", function(){
console.log("end",arguments)
});
response.on("finish", function(){
console.log("finish", arguments)
});
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
response.end(body);
});
}).listen(8080);
Once started, I invoke it with a simple curl call.
curl -d "echo" -X POST http://localhost:8080
It turn's out that the data
event isn't triggered. I didn't find any other way of getting the body via response object. How can I do that?