1

I am using node-http-proxy for the POST request as follows:

route.js
---------

var express = require('express');
var httpProxy = require('http-proxy');
var bodyParser = require('body-parser');
var proxy = httpProxy.createProxyServer({secure:false});
var jsonParser = bodyParser.json();

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    logger.debug("proxying for",req.url);
    //set headers
    logger.debug('proxy request forwarded succesfully');
});

proxy.on('error', function (err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });
  res.end('Something went wrong. And we are reporting a custom error message.');
});

proxy.on('proxyRes', function (proxyRes, req, res) {
  console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});

module.exports = function(app){
  app.post('/recording',jsonParser,function(req,res){
    // update request body
    proxy.web(req, res, { target: <<host>>:<<port>>});
  });
}

app.js
---------

var express = require('express');
var app = express();
 require('./routes')(app);

app.listen(8080);
console.log("Demo server running");

I also use bodyparser middleware and it has a known issue as mentioned in Gitbug issue. So I tried adding this line as the last line in app.js

app.use(require('connect-restreamer')());

But still the POST request hangs and ultimately fails. How do I fix this ? Is there any alternatives for bodyparser ?

cucucool
  • 3,777
  • 8
  • 48
  • 63

2 Answers2

3

Try reversing the order of the bodyParser- and proxy middleware:

module.exports = function(app){
  app.post('/recording', function(req,res){
    // update request body
    proxy.web(req, res, { target: <<host>>:<<port>>});
  }, jsonParser);
}

Think this issue is similar to: socket hang up error with nodejs.

Community
  • 1
  • 1
chimurai
  • 1,285
  • 1
  • 16
  • 18
  • When I receive a POST request for "/recording" I need to parse the JSON from the request body and then proxy it to another server. When I move the middleware after the proxy, I could not extract the JSON from the initial request as it says undefined. – cucucool Feb 15 '16 at 02:02
  • In that case you'll probably have to use https://github.com/dominictarr/connect-restreamer to restore the stream; since bodyParser is messing them up. – chimurai Feb 15 '16 at 14:43
2

To expand on this a bit, what's happening here is that the node request is a stream, it can only be read once, after that the stream data is consumed.

When you use body-parser middleware in express, it will consume the request stream body - if you try to proxy the request after this, there's no body stream to send, so the other end of the proxy receives a POST with a content-length, etc... but waits indefinitely to receive the POST body which never arrives.

If you want to proxy POST/PUT or really any requests that contain a body, you have to do that before any middleware consumes the body. That's why @chimmurai answer above works.

Also, be aware that for the same reason, middleware that executes after you proxy a request will be affected the same way, once the request stream is consumed there won't be anything for subsequent middleware to read. That's the reason for things like connect-restreamer.

Clayton Gulick
  • 9,755
  • 2
  • 36
  • 26