node express/request: piping a POST request with body parsing node express/request: piping a POST request with body parsing express express

node express/request: piping a POST request with body parsing


The issue is that with streams (like req), once you've read it you can't reset it.

Because body-parser has read the stream already, piping it won't work because that will try to read the stream again (which at that point has been exhausted).

A workaround would be take the text read by body-parser, and create a minimal req "clone" in order for request to be able to pass the request along:

var intoStream = require('into-stream');var bodyParser = require('body-parser');var textParser = bodyParser.text({ limit: '50kb' });var postRouteHandler = function(req, res) {  let text = req.body;  if (! shouldPipe(text)) {    return res.sendStatus(400); // or whatever  }  // Here's where the magic happens: create a new stream from `text`,  // and copy the properties from `req` that `request` needs to pass  // along the request to the destination.  let stream     = intoStream(text);  stream.method  = req.method;  stream.headers = req.headers;  // Pipe the newly created stream to request.  stream.pipe(request(url)).pipe(res);};app.use('/postRoute', [textParser, postRouteHandler]);


You should create your own middleware if the parser is causing the problem. In all honesty you may want to parse the body a different way than the body parser.

Given the limited knowledge about what the rest of your project is doing you could create middleware to hook the body parser middleware and just send a clone of request. This however is not very efficient but will work and may help point you in the right direction.

var postRouteHandler = someFunction;var bodyParser = require('body-parser');var textParseMiddleware = function (req, res, next) {  var requestclone = _.clone(req);  var textParser = bodyParser.text({    limit: '50kb'  });  textParser(requestclone, res, function(){    console.log('examine the body here', requestclone.body);  });}app.use('/postRoute', [textParserMiddleWare, postRouteHandler]);

I have not tested the above code.


Need to send a huge large data via POST, I was getting Error: socket hang up / code: 'ECONNRESET'.Was able to resolve it via increasing limit in body parser.

var bodyParser = require('body-parser');app.use(bodyParser.urlencoded({extended: true}));app.use(bodyParser.json({limit:'50mb'}));