I have sample code as following.
var getPostedData = function (request) {
var qs = require('querystring');
var body = '';
request.on('data', function(chunk) {
body += chunk.toString();
});
return qs.parse(body);
};
getPostedData();
But the connection request data is coming as stream or multiple data packets. It cannot be returned as like above code in node v0.12. The sample code will work with node v0.10.
One solution to multiple data packets is to listen end
event emit on request object.
var getPostedData = function (request) {
var qs = require('querystring');
var body = '';
request.on('data', function(chunk) {
body += chunk.toString();
}).on('end', function() {
//Here body will have multiple data packets
//return qs.parse(body);
});
return qs.parse(body);
};
getPostedData();
But here we can't get return value because of asynchronous nature. How to return full multiple data chunks in a function/method?