proxy json requests with node express proxy json requests with node express json json

proxy json requests with node express


You need to move that custom middleware function before this line so that it's executed before any of the body parsers. This makes sure that the request data is still there for piping to request(url) in your custom middleware.

The cause of the hanging currently is that req has no data to write to request(url) (because the body parsing middleware already read all of the request data and parsed it) and so it never calls .end() on the request(url) stream. This means that the request to url never completes because it's just sitting there waiting for data that it will never get.


In the case of a post request, the following construct works:

app.post('/api/method', (req, res) => {  req.pipe(request.post(someUrl, { json: true, body: req.body }), { end: false }).pipe(res);}

This is of course relevant if you're using the bodyparser middleware.


app.use('/api', function(req, res) {  var url = 'http://my.domain.com/api' + req.url;    request({          uri: url,          method: "POST",          body: _body,          json: true      }, function (_err, _res, _resBody) {          //do somethings          res.json(_resBody);      });});