Skip to content

Commit

Permalink
Fix correct detection of request end
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasdoerflerkrone committed Jun 23, 2023
1 parent 432ca14 commit f4756b0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,24 @@ app.all('*', (req, res, next) => {

http
.request(options, resp => {
// log the data
resp.once("data", d => {
const chunks = [];
resp.on('data', data => chunks.push(data));
resp.on('end', () => {
let resBody = Buffer.concat(chunks);
switch (resp.headers['content-type']) {
case 'application/json':
resBody = JSON.parse(resBody);
break;
}
console.log("Got response for call: http://%s:%d%s -> %d: %s", options.hostname, options.port, options.path, resp.statusCode, resp.statusMessage);
res({
...options,
statusCode: resp.statusCode,
statusMessage: resp.statusMessage
statusMessage: resp.statusMessage,
body: resBody.toString()
})
});

})
})
.on("error", err => {
console.log("Error: " + err.message);
Expand All @@ -62,7 +71,8 @@ app.all('*', (req, res, next) => {
...options,
err: err
})
}).end();
})
.end();
}));
});

Expand Down

0 comments on commit f4756b0

Please sign in to comment.