From d4659d2de75600e85ff3d2eddbe42944221ef8b1 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 15 Sep 2014 18:00:43 -0700 Subject: [PATCH] Remove req.body initialization to {} --- HISTORY.md | 2 ++ README.md | 2 +- lib/types/json.js | 4 +++- lib/types/raw.js | 4 +++- lib/types/text.js | 4 +++- lib/types/urlencoded.js | 4 +++- test/body-parser.js | 15 ++++++++++----- test/json.js | 15 ++++++++++----- test/raw.js | 4 ++-- test/text.js | 13 +++++++++---- test/urlencoded.js | 13 +++++++++---- 11 files changed, 55 insertions(+), 25 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0853de0b..e595c4ab 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,8 @@ 2.x === + * `req.body` is no longer always initialized to `{}` + - it is left `undefined` unless a body is parsed * `urlencoded` parser now defaults `extended` to `false` * Use `on-finished` to determine when body read diff --git a/README.md b/README.md index 980e1731..0bd9d25b 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding ### req.body -A new `body` object containing the parsed data is populated on the `request` object after the middleware. +A new `body` object containing the parsed data is populated on the `request` object after the middleware. If there was no body to parse or the request was of a type there was no parser for, then `req.body` will just be `undefined`. ## Examples diff --git a/lib/types/json.js b/lib/types/json.js index 02864915..e83df1ac 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -82,7 +82,9 @@ function json(options) { return next() } - req.body = req.body || {} + if (!('body' in req)) { + req.body = undefined + } if (!typeis(req, type)) return next() diff --git a/lib/types/raw.js b/lib/types/raw.js index 8dea155e..06b7e380 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -50,7 +50,9 @@ function raw(options) { return next() } - req.body = req.body || {} + if (!('body' in req)) { + req.body = undefined + } if (!typeis(req, type)) return next() diff --git a/lib/types/text.js b/lib/types/text.js index 58bff5d9..b66cb665 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -52,7 +52,9 @@ function text(options) { return next() } - req.body = req.body || {} + if (!('body' in req)) { + req.body = undefined + } if (!typeis(req, type)) return next() diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index 61f0baf6..5bae00bc 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -65,7 +65,9 @@ function urlencoded(options){ return next() } - req.body = req.body || {} + if (!('body' in req)) { + req.body = undefined + } if (!typeis(req, type)) return next(); diff --git a/test/body-parser.js b/test/body-parser.js index 7cd0ca46..1170c94e 100644 --- a/test/body-parser.js +++ b/test/body-parser.js @@ -11,10 +11,10 @@ describe('bodyParser()', function(){ server = createServer() }) - it('should default to {}', function(done){ + it('should default req.body to undefined', function(done){ request(server) .post('/') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) it('should parse JSON', function(done){ @@ -105,9 +105,14 @@ function createServer(opts){ var _bodyParser = bodyParser(opts) return http.createServer(function(req, res){ - _bodyParser(req, res, function(err){ - res.statusCode = err ? (err.status || 500) : 200; - res.end(err ? err.message : JSON.stringify(req.body)); + _bodyParser(req, res, function (err) { + if (err) { + res.statusCode = err.status || 500 + res.end(err.message) + } + + res.statusCode = 200; + res.end(JSON.stringify(req.body) || typeof req.body); }) }) } diff --git a/test/json.js b/test/json.js index c217b81b..9e900ccb 100644 --- a/test/json.js +++ b/test/json.js @@ -53,7 +53,7 @@ describe('bodyParser.json()', function(){ .get('/') .set('Content-Type', 'application/json') .unset('Transfer-Encoding') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) it('should 400 on malformed JSON', function(done){ @@ -259,7 +259,7 @@ describe('bodyParser.json()', function(){ .post('/') .set('Content-Type', 'application/json') .send('{"user":"tobi"}') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) }) @@ -460,9 +460,14 @@ function createServer(opts){ var _bodyParser = bodyParser.json(opts) return http.createServer(function(req, res){ - _bodyParser(req, res, function(err){ - res.statusCode = err ? (err.status || 500) : 200; - res.end(err ? err.message : JSON.stringify(req.body)); + _bodyParser(req, res, function (err) { + if (err) { + res.statusCode = err.status || 500 + res.end(err.message) + } + + res.statusCode = 200; + res.end(JSON.stringify(req.body) || typeof req.body); }) }) } diff --git a/test/raw.js b/test/raw.js index 700d3b6e..96b5054a 100644 --- a/test/raw.js +++ b/test/raw.js @@ -165,7 +165,7 @@ describe('bodyParser.raw()', function(){ var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') test.write(new Buffer('000102', 'hex')) - test.expect(200, '{}', done) + test.expect(200, 'undefined', done) }) }) @@ -305,7 +305,7 @@ function createServer(opts){ return } - res.end(JSON.stringify(req.body)) + res.end(JSON.stringify(req.body) || typeof req.body) }) }) } diff --git a/test/text.js b/test/text.js index 1729de99..62b6d5e4 100644 --- a/test/text.js +++ b/test/text.js @@ -192,7 +192,7 @@ describe('bodyParser.text()', function(){ .post('/') .set('Content-Type', 'text/plain') .send('user is tobi') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) }) @@ -352,9 +352,14 @@ function createServer(opts){ var _bodyParser = bodyParser.text(opts) return http.createServer(function(req, res){ - _bodyParser(req, res, function(err){ - res.statusCode = err ? (err.status || 500) : 200; - res.end(err ? err.message : JSON.stringify(req.body)); + _bodyParser(req, res, function (err) { + if (err) { + res.statusCode = err.status || 500 + res.end(err.message) + } + + res.statusCode = 200; + res.end(JSON.stringify(req.body) || typeof req.body); }) }) } diff --git a/test/urlencoded.js b/test/urlencoded.js index a5778f0f..5e2c2b9d 100644 --- a/test/urlencoded.js +++ b/test/urlencoded.js @@ -347,7 +347,7 @@ describe('bodyParser.urlencoded()', function(){ .post('/') .set('Content-Type', 'application/x-www-form-urlencoded') .send('user=tobi') - .expect(200, '{}', done) + .expect(200, 'undefined', done) }) }) @@ -517,9 +517,14 @@ function createServer(opts){ var _bodyParser = bodyParser.urlencoded(opts) return http.createServer(function(req, res){ - _bodyParser(req, res, function(err){ - res.statusCode = err ? (err.status || 500) : 200; - res.end(err ? err.message : JSON.stringify(req.body)); + _bodyParser(req, res, function (err) { + if (err) { + res.statusCode = err.status || 500 + res.end(err.message) + } + + res.statusCode = 200; + res.end(JSON.stringify(req.body) || typeof req.body); }) }) }