Friday, 17 March 2017

Proxing request to remote server

Am trying to proxy a request from my angular client app via express.js. This is middleware

app.use('/v1', function(req, res) {  
  var url = process.env.API_URL + req.url;
  console.info('Proxing to: ', url);

  var r = null;

  switch(req.method) {
      case 'POST':
        r = request.post({uri: url, json: req.body });
        break;
      case 'PUT':
        r = request.put({uri: url, json: req.body });
        break;

      default:
        r = request(url);
  }  

  console.log(r);

  return req.pipe(r).pipe(res);
});

The express server is running at http://localhost:3030 . The middleware works for GET requests to http://localhost:3030/v1, but it doesn't work for POST or PUT requests. The server is killed when i make a PUT or POST request. This is the error

stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^

Error: write after end
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:426:15)
    at Request.write (/Users/user/Projects/maxmc/node_modules/request/request.js:1514:27)
    at end (/Users/user/Projects/maxmc/node_modules/request/request.js:552:18)
    at Immediate._onImmediate (/Users/user/Projects/maxmc/node_modules/request/request.js:581:7)
    at tryOnImmediate (timers.js:543:15)
    at processImmediate [as _immediateCallback] (timers.js:523:5)
error Command failed with exit code 1.

I can't figure what the issue is.



via MrFoh

No comments:

Post a Comment