Skip to content

Commit f4756b0

Browse files
Fix correct detection of request end
1 parent 432ca14 commit f4756b0

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

app.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,24 @@ app.all('*', (req, res, next) => {
4545

4646
http
4747
.request(options, resp => {
48-
// log the data
49-
resp.once("data", d => {
48+
const chunks = [];
49+
resp.on('data', data => chunks.push(data));
50+
resp.on('end', () => {
51+
let resBody = Buffer.concat(chunks);
52+
switch (resp.headers['content-type']) {
53+
case 'application/json':
54+
resBody = JSON.parse(resBody);
55+
break;
56+
}
5057
console.log("Got response for call: http://%s:%d%s -> %d: %s", options.hostname, options.port, options.path, resp.statusCode, resp.statusMessage);
5158
res({
5259
...options,
5360
statusCode: resp.statusCode,
54-
statusMessage: resp.statusMessage
61+
statusMessage: resp.statusMessage,
62+
body: resBody.toString()
5563
})
56-
});
64+
65+
})
5766
})
5867
.on("error", err => {
5968
console.log("Error: " + err.message);
@@ -62,7 +71,8 @@ app.all('*', (req, res, next) => {
6271
...options,
6372
err: err
6473
})
65-
}).end();
74+
})
75+
.end();
6676
}));
6777
});
6878

0 commit comments

Comments
 (0)