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?