Unzip the POST body using node + express

I have a simple node application that should write metrics from clients. Clients send json-formatted metrics archived by the python zlib module, I'm trying to add middleware to unzip the request message before express tele-servicing happens.

My resellers are simply the default ones:

app.configure(function(){ app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser('your secret here')); app.use(express.session()); app.use(app.router); app.use(require('less-middleware')({ src: __dirname + '/public' })); app.use(express.static(path.join(__dirname, 'public'))); }); 

I tried adding simple middleware that receives data and then unpacks it:

 app.use(function(req, res, next) { var data = ''; req.addListener("data", function(chunk) { data += chunk; }); req.addListener("end", function() { zlib.inflate(data, function(err, buffer) { if (!err) { req.body = buffer; next(); } else { next(err); } }); }); }); 

Problem with zlib.inflate I get this error:

 Error: incorrect header check 

Data is compressed using the python zlib module:

 zlib.compress(jsonString) 

but it seems that do not unpack, do not inflate, do not start gunzip.

+6
source share
2 answers

I found the solution myself, the problem was in this piece of code:

 req.addListener("data", function(chunk) { data += chunk; }); 

It seems that the concatenation of the request data is incorrect, so I switched my middleware to this:

 app.use(function(req, res, next) { var data = []; req.addListener("data", function(chunk) { data.push(new Buffer(chunk)); }); req.addListener("end", function() { buffer = Buffer.concat(data); zlib.inflate(buffer, function(err, result) { if (!err) { req.body = result.toString(); next(); } else { next(err); } }); }); }); 

Buffer concatenation works fine, and now I can get the request body, which is unpacked.

+12
source

I know this is a very late answer, but with the body-parser module it will be:

Returns middleware that only json processes. This analyzer accepts any Unicode encoding for the body and supports automatic gzip inflation and encoding deflation.

 var bodyParser = require('body-parser'); app.use( bodyParser.json() ); // to support JSON-encoded bodies 
+2
source

All Articles