1

Ultimately I am just trying to POST an image from the browser to a server. Unfortunately running into CORS issues, so for the moment, attempting to use our Node.js server as a proxy server.

I have this:

router.post('/image', function (req, res, next) {

  const filename = uuid.v4();

  const proxy = http.request({
    method: 'PUT',
    hostname: 'engci-maven.nabisco.com',
    path: `/artifactory/cdt-repo/folder/${filename}`,
    headers: {
      'Authorization': 'Basic ' + Buffer.from('foo:bar').toString('base64'),
    }
  });

  req.pipe(proxy).pipe(res).once('error', next);

});

the browser initiates the request, but I get an error in the browser saying I get an empty response, the error is:

enter image description here

Does anyone know why this error might occur? Is there something wrong with my proxy code in Node.js? Authorization should be fine, and the requeset url should be fine. Not sure what's going on.

3
  • note that I put the proxy request before any body parsing, as initially that was causing problems because it was reading the request data before passing it on to the image server. Commented Jul 29, 2017 at 3:14
  • Is there no error in node's console? Commented Jul 29, 2017 at 3:15
  • 2
    there was no error because the response was being proxied, the response just wasn't coming back for some reason. I found an answer. But still not sure why it works. I put a question on Nodejs Help channel: github.com/nodejs/help/issues/760 Commented Jul 29, 2017 at 3:54

1 Answer 1

1

Ok so this worked, but I am not really sure why:

router.post('/image', function (req, res, next) {

  const filename = uuid.v4();

  const proxy = http.request({
    method: 'PUT',
    hostname: 'engci-maven.nabisco.com',
    path: `/artifactory/cdt-repo/folder/${filename}`,
    headers: {
      'Authorization': 'Basic ' + Buffer.from('foo:bar').toString('base64'),
    }
  }, function(resp){

    resp.pipe(res).once('error', next);

  });

  req.pipe(proxy).once('error', next);

});

There is an explanation for why this works on this Node.js help thread: https://github.com/nodejs/help/issues/760

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.