middleware for saving raw post data in the request object won't "next" and cause timeout

express, node.js, post

Solution

Call next() without waiting for 'end', to prevent bodyParser and your code from interfering.

app.use(function(req, res, next) {
  req.rawBody = '';

  req.on('data', function(chunk) { 
    req.rawBody += chunk;
  });

  next();
});
app.use(express.bodyParser());

Kudos to https://stackoverflow.com/a/21222540/1891397

Problem

I need to get the raw post data for a specific endpoint in a node/express app. I do: ``` app.use('/paypal/ipn',function(req, res, next) { var postData=''; req.on('data', function(chunk) { postData += chunk; }); req.on('end', function() { req.rawBody = postData; console.log('ended buffering. result: ' + req.rawBody); next(); }); }); ``` What happens is that I get the console.log output in the console and then nothing happens. After a minute or so I see the server returns 200 - probably a timeout. It's like the next() command never executes, or executes and stales. When I comment out everything, and simply call next(): ``` app.use('/paypal/ipn',function(req, res, next) { /* var postData=''; req.on('data', function(chunk) { postData += chunk; }); req.on('end', function() { req.rawBody = postData; console.log('ended buffering. result: ' + req.rawBody); next(); }); */ next(); }); ``` Everything works, that is the endpoint is called (of course the request doesn't contain the rawBody). So it seems like I'm doing something wrong the way I buffer the rawBody? Something that causes next() not to work?

Original source

Related problems