문제

I have a proxy that works well (as far as I can tell) UNTIL I attempt to proxy images (or perhaps any binary data?). By my estimations, the below code should work, but it does not. I'm sure I"m doing something obviously dumb, but I've been digging through forums and apis and I have yet to hit upon the correct approach.

The 'core' of my proxy looks something like the following.

  function(req, res) {
    ...

    options = {
      url: 'a url',
      headers: {
        'Authorization': auth
      }
    };

    request(options,
      function(e, r, b){
        var encoding = (r.headers['content-type'].indexOf('image') === -1) ? 'utf8' : 'binary';

        res.writeHead(200, {
          'Content-Length': r.headers['content-length'],
          'Content-Type': r.headers['content-type']
        });

        if (encoding === 'binary') {
          b = new Buffer(b);
        }

        res.end(b, encoding);
      });
    }

What am I missing here?

Thanks in advance for any and all help!

도움이 되었습니까?

해결책

My problem was not with the response (as I though originally), but rather the fact that the request module was encoding it's response body to unicode by default, when disabled (encoding: null), the response body is converted to a buffer which is easily consumed by the response.

    options = {
      url: url,
      encoding: null,
      headers: {
        'Authorization': auth
      }
    };

    request(options,
      function(e, r, b){
        var encoding = (r.headers['content-type'].indexOf('image') === -1) ? 'utf8' : 'binary';
        res.end(b, encoding);
      });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top