Skip to content

Commit

Permalink
Remove req.body initialization to {}
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Oct 30, 2014
1 parent 28ef179 commit d4659d2
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 25 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
15 changes: 10 additions & 5 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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);
})
})
}
15 changes: 10 additions & 5 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('bodyParser.json()', function(){
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -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);
})
})
}
4 changes: 2 additions & 2 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -305,7 +305,7 @@ function createServer(opts){
return
}

res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
})
})
}
13 changes: 9 additions & 4 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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);
})
})
}
13 changes: 9 additions & 4 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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);
})
})
}
Expand Down

0 comments on commit d4659d2

Please sign in to comment.