Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST proxying with bodyParser middleware #955

Closed
thisiskarthikj opened this issue Feb 10, 2016 · 3 comments
Closed

POST proxying with bodyParser middleware #955

thisiskarthikj opened this issue Feb 10, 2016 · 3 comments

Comments

@thisiskarthikj
Copy link

The following is my code.

route.js

var express = require('express');
var httpProxy = require('http-proxy');
var bodyParser = require('body-parser');
var proxy = httpProxy.createProxyServer({secure:false});
var app = express();
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");

But still the POST request hangs and ultimately fails.

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

I also tried restreamer code but I get the following error if I use the restreamer

error: uncaughtException: Can't set headers after they are sent.
I get this error because I emit end in the restreamer and then try to set the headers in proxy.on function. Any suggestions appreciated.

@thisiskarthikj
Copy link
Author

I manage to work around the issue. But now I am facing an unique issue. The content seems to be truncated when the request is proxied to the server. I tried setting content-length to the body length and it does not work. Any suggestions ?

@pyper
Copy link

pyper commented Feb 11, 2016

It maybe be how you are measuring the length. That is the encoding may throw that off. I don’t think you need have a content-length specified. If not, just take the header out altogether. Also, make sure it is actually getting set. Validate that the destination server actually gets the header with the length you set. What I think is happening for you in the bodyParser middleware is mutating the content, and thus the length, before it goes to the destination server.

@narciero
Copy link

@karthikus How did you work around the issue?

matibek added a commit to matibek/node-http-proxy that referenced this issue Jun 23, 2016
Support for bodyparser.json and bodyparser.urlencoded.

Fixes http-party#955 http-party#843 http-party#791
jcrugzz pushed a commit that referenced this issue Aug 11, 2016
Support for bodyparser.json and bodyparser.urlencoded.

Fixes #955 #843 #791
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants