Middleware to save the original message data in the request object will not be "next" and will cause a timeout

I need to get the source data for a specific endpoint in a node / express application. I:

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 console.log output in the console, and then nothing happens. After a minute or so, I see that the server returns 200 - maybe a timeout. This is similar to the next () command, which never executes or never executes or is not put.

When I comment on everything and just 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 does not contain rawBody).

So it seems like I'm doing something wrong, how am I rawBody buffer? Something that calls next () does not work?

+4
4

next(), "end", bodyParser .

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

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

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

fooobar.com/questions/170591/...

+6

, (rawBody):

/*
for some very strange reason, while a IE8/IE9 use a XDomain for CORS requests, express.js bodyParser fail to parse
*/
app.use(function(req, res, next) {
    if(req.method !== 'POST') {
        next();
        return;
    }
    var data = '';
    req.setEncoding('utf8');
    req.on('data', function(chunk) { 
        data += chunk;
    });
    req.on('end', function() {
        req.rawBody = data;
        req.failSafeBody = queryParser(data);
        next();
    });
});
//se above
//app.use(express.bodyParser());
app.use(function(req, res, next){
    if(req.method === 'POST' && (!req.body || Object.keys(req.body).length<1) ) {
        req.body = req.failSafeBody;
    }
    next();
});
app.use(express.methodOverride());
+1

, :

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);
    console.log(next);
  });
});

, . , .

+1

. , , Google , , .

, connect node_module , express.urlencoded(), :

app.use(express.urlencoded());
app.use(function(req, res, next){
    if (req.method !== 'POST' || JSON.stringify(req.body) !== JSON.stringify({})){
        next()
        return
    }
    var data = ''
    req.on('data', function(chunk){
        data += chunk
    })
    req.on('end', function(){
        req.rawBody = data
        next()
    })
})

? -, , urlencoded(), req.body .

rawBody , POST (GET?), req.body {}, , urlencoded() , ( rawBody , () req.body '&'), next()

(sr , , , , ;))

0
source

All Articles