Browse files

Simplified the middleware function

  • Loading branch information...
1 parent 40c657f commit 3a6ac13b6c017fee54171d566bd6012e1ab5ded3 @fgnass committed May 31, 2012
Showing with 6 additions and 32 deletions.
  1. +6 −32 lib/form2json.js
View
38 lib/form2json.js
@@ -88,45 +88,19 @@ exports.transform = function(obj) {
return pairs.reduce(fillIn(), {})
}
-var rplus = /\+/g
exports.decode = function(data) {
var body = qs.parse(data)
return exports.transform(body)
};
/**
- * Connect middleware that adds a `req.json` property.
- * @param flatBody If `true`, connect's default decoder for
- * application/x-www-form-urlencoded data is overwritten with
- * `querystring.parse`. Hence `req.body` will be a *flat* object,
- * whereas `req.json` will be nested object graph.
+ * Middleware that adds a `req.json` property.
*/
-exports.middleware = function(flatBody) {
-
- if (flatBody) {
- require('connect').middleware.bodyDecoder.decode['application/x-www-form-urlencoded'] = qs.parse
- }
-
- return function form2json(req, res, next) {
- var body;
- if (!req.json) {
- try {
- if (flatBody) {
- body = req.body
- }
- else {
- body = qs.parse(rawBody)
- }
- req.json = body ? exports.transform(body) : {}
- }
- catch (err) {
- return next(err)
- }
- next()
- }
- else {
- next()
- }
+exports.middleware = function form2json(req, res, next) {
+ if (!req.json) {
+ var flat = req.body || req.query
+ req.json = flat ? exports.transform(body) : {}
}
+ next()
}

0 comments on commit 3a6ac13

Please sign in to comment.