Skip to content

Instantly share code, notes, and snippets.

@luebken
Created August 2, 2010 10:43
Show Gist options
  • Save luebken/504459 to your computer and use it in GitHub Desktop.
Save luebken/504459 to your computer and use it in GitHub Desktop.
/*!
* Ext JS Connect
* Copyright(c) 2010 Sencha Inc.
* MIT Licensed
*/
/**
* Module dependencies.
*/
var queryString = require('querystring');
/**
* Extract the mime type from the given request's
* _Content-Type_ header.
*
* @param {IncomingMessage} req
* @return {String}
* @api private
*/
function mime(req) {
var str = req.headers['content-type'] || '';
return str.split(';')[0];
}
/**
* Supported decoders.
*
* - application/x-www-form-urlencoded
* - application/json
*/
exports.decode = {
'application/x-www-form-urlencoded': queryString.parse,
//'application/xml': JSON.parse, //PATCH: Add this to get auto decoding for your application type
'application/json': JSON.parse
};
/**
* Decode request bodies.
*
* @return {Function}
* @api public
*/
module.exports = function bodyDecoder(){
return function bodyDecoder(req, res, next) {
var decoder = exports.decode[mime(req)];
if (decoder) {
var data = '';
req.setEncoding('utf8');
req.addListener('data', function(chunk) { data += chunk; });
req.addListener('end', function() {
req.rawBody = data;
try {
req.body = data
? decoder(data)
: {};
} catch (err) {
return next(err);
}
next();
});
} else {
//PATCH: start
var data = '';
req.addListener('data', function(chunk) { data += chunk; });
req.addListener('end', function() {
req.rawBody = data;
next();
});
//PATCH: end
//next(); //uncomment to unpatch
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment