Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created May 31, 2012 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 Raynos/2843615 to your computer and use it in GitHub Desktop.
Save Raynos/2843615 to your computer and use it in GitHub Desktop.
var StringDecoder = require("string_decoder").StringDecoder
module.exports = body
function body(req, callback) {
var requestBody = "",
stringDecoder = new StringDecoder
req.on("data", addToBody)
req.on("end", returnBody)
function addTobody(buffer) {
requestBody += stringDecoder.write(buffer)
}
function returnBody() {
callback(null, requestBody)
}
}
var StringDecoder = require("string_decoder").StringDecoder,
BodyMap = new WeakMap
module.exports = body
function body(req, callback) {
if (BodyMap.has(req)) {
callback(null, BodyMap.get(req))
}
var requestBody = "",
stringDecoder = new StringDecoder
req.on("data", addToBody)
req.on("end", returnBody)
function addTobody(buffer) {
requestBody += stringDecoder.write(buffer)
}
function returnBody() {
BodyMap.set(req, requestBody)
callback(null, requestBody)
}
}
var StringDecoder = require("string_decoder").StringDecoder
module.exports = body
function body(req, callback) {
if (req.body) {
callback(null, req.body)
}
var requestBody = "",
stringDecoder = new StringDecoder
req.on("data", addToBody)
req.on("end", returnBody)
function addTobody(buffer) {
requestBody += stringDecoder.write(buffer)
}
function returnBody() {
req.body = requestBody
callback(null, requestBody)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment