4

i am trying to make an HTTP proxy usng nodejitsu/node-http-proxy and i got this methods to catch proxied responses

proxy.on('proxyRes', function (proxyRes, req, res) {
    var body = new Buffer('');
    proxyRes.on('data', function (data) {
        body = Buffer.concat([body, data]);
    });
    proxyRes.on('end', function () {
        body = body.toString();
        console.log("res from proxied server:", body);
     });
});

here i able to get response but it is in buffer. so i tried many methods to convert my response to string format

but i got this instead res from proxied server: �Ao�@���j/�Rc�I\���[ɡ�P�^O�m��hg������"�ĉ�J�i�y��g��K��y��%z� �j:�J�5/�g]�B��

how to solve this issue

thanks in advance

inval
  • 380
  • 1
  • 4
  • 11
  • [`new Buffer` is deprecated](https://nodejs.org/api/buffer.html#buffer_new_buffer_string_encoding). Also have you considered that your content does not use the [default encoding](https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end)? – Bergi Sep 17 '18 at 19:31
  • @Bergi hi i tried toString('utf8') and ascii but did't work, and my response from proxy server is a json string – inval Sep 17 '18 at 19:37

0 Answers0