Skip to content

Instantly share code, notes, and snippets.

@broofa
Created May 12, 2010 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broofa/398620 to your computer and use it in GitHub Desktop.
Save broofa/398620 to your computer and use it in GitHub Desktop.
server.addListener('request', function(req, res) {
var chunks = [], bytes = 0;
req.addListener('data', function(chunk) {
// Gather up chunks & track total data length
chunks.push(chunk);
bytes += chunk.length;
});
// "req.getBody(encoding)" returns the body string
req.getBody = function(encoding) {
// Write all chunks into a Buffer
var body = new Buffer(bytes), offset=0;
chunks.forEach(function(chunk) {
chunk.copy(body, offset, 0, chunk.byteLength);
offset += chunk.byteLength;
});
// Return encoded (default to UTF8) string
return body.toString(encoding || 'utf8', 0, body.byteLength);
};
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment