Skip to content

Commit

Permalink
updated parseJson middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
nrstott committed Jan 9, 2012
1 parent 8615aa8 commit 75391c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions lib/middleware.js
Expand Up @@ -26,21 +26,23 @@ function join(forEachable) {
*
* @returns {Function} JSGI Middleware
*/
exports.parseJson = exports.ParseJson = function(req, nextApp) {
var contentType = req.headers['content-type'];
exports.parseJson = exports.ParseJson = function() {
return function(req, nextApp) {
var contentType = req.headers['content-type'];

if (contentType && req.body) {
if (contentType === "application/json") {
if (contentType && req.body) {
if (contentType === "application/json") {

return when(join(req.body), function success(body) {
req.body = JSON.parse(body);
return when(join(req.body), function success(body) {
req.body = JSON.parse(body);

return nextApp(req);
});
return nextApp(req);
});
}
}
}

return nextApp(req);
return nextApp(req);
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/middleware.test.js
Expand Up @@ -34,7 +34,7 @@ test("test parses JSON", function(t) {
request.body = body;

process.nextTick(function() {
bogart.middleware.parseJson(request, function(req) {
bogart.middleware.parseJson()(request, function(req) {
t.ok(!!req);
t.equal('1', req.body.a, 'req.body.a should equal "1"');
t.end();
Expand Down

0 comments on commit 75391c1

Please sign in to comment.