From 0a877961b2678d424f24a81c5392244a762fd3cc Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 19 Jun 2013 16:42:03 -0500 Subject: [PATCH] move parseJson to its own file Moves parseJson middleware to its own file. This requires moving the join helper function that is used by parseJson and parseForm to a seperate file as well. This new file is forEachable.js and is to hold utility functions for working with forEachables. --- lib/forEachable.js | 13 +++++++++++++ lib/middleware.js | 32 ++------------------------------ lib/middleware/parseJson.js | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 lib/forEachable.js create mode 100644 lib/middleware/parseJson.js diff --git a/lib/forEachable.js b/lib/forEachable.js new file mode 100644 index 0000000..c775737 --- /dev/null +++ b/lib/forEachable.js @@ -0,0 +1,13 @@ +var q = require('./q'); + +exports.join = function (forEachable) { + var body = ''; + + function appendChunk(chunk) { + body += chunk; + }; + + return q.when(forEachable.forEach(appendChunk), function () { + return body; + }); +}; \ No newline at end of file diff --git a/lib/middleware.js b/lib/middleware.js index c8866b7..0d216a2 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -19,19 +19,6 @@ var middleware = function (handler) { module.exports = middleware; -function join(forEachable) { - var body = '', - appendChunk; - - appendChunk = function(chunk) { - body += chunk; - }; - - return when(forEachable.forEach(appendChunk), function success() { - return body; - }); -} - function deprecate(oldName, newName, fn) { return function () { console.log(oldName + ' is deprecated, please use ' + newName + ' instead'); @@ -39,22 +26,7 @@ function deprecate(oldName, newName, fn) { } } -middleware.parseJson = middleware(function (req, nextApp) { - var contentType = req.headers['content-type']; - - if (contentType && req.body) { - if (contentType === "application/json") { - - return when(join(req.body), function success(body) { - req.body = JSON.parse(body); - - return nextApp(req); - }); - } - } - - return nextApp(req); -}); +middleware.parseJson = require('./middleware/parseJson'); middleware.ParseJson = deprecate('ParseJson', 'parseJson', middleware.parseJson); @@ -69,7 +41,7 @@ middleware.parseForm = middleware.ParseForm = function(nextApp) { if (contentType === 'application/x-www-form-urlencoded' && req.body) { - return when(join(req.body), function(body) { + return when(require('./forEachable').join(req.body), function(body) { req.body = require('querystring').parse(body); return nextApp(req); diff --git a/lib/middleware/parseJson.js b/lib/middleware/parseJson.js new file mode 100644 index 0000000..926484a --- /dev/null +++ b/lib/middleware/parseJson.js @@ -0,0 +1,20 @@ +var middleware = require('../middleware') + , forEachable = require('../forEachable') + , q = require('../q'); + +module.exports = middleware(function (req, nextApp) { + var contentType = req.headers['content-type']; + + if (contentType && req.body) { + if (contentType === 'application/json') { + + return q.when(forEachable.join(req.body), function success(body) { + req.body = JSON.parse(body); + + return nextApp(req); + }); + } + } + + return nextApp(req); +}); \ No newline at end of file