Created
May 12, 2010 14:05
-
-
Save broofa/398620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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