Skip to content

Commit

Permalink
Fix res.send(status, num) to send num as json
Browse files Browse the repository at this point in the history
fixes #2226
  • Loading branch information
ysmood authored and dougwilson committed Jul 11, 2014
1 parent cf8005e commit 544c666
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
3.x
===

* fix `res.send(status, num)` to send `num` as json (not error)
* deps: basic-auth@1.0.0
- support empty password
- support empty username
Expand Down
15 changes: 9 additions & 6 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ res.send = function(body){
}
}

// disambiguate res.send(status) and res.send(status, num)
if (typeof body === 'number' && arguments.length === 1) {
// res.send(status) will set status message as text string
this.get('Content-Type') || this.type('txt');
this.statusCode = body;
body = http.STATUS_CODES[body];
}

switch (typeof body) {
// response status
case 'number':
this.get('Content-Type') || this.type('txt');
this.statusCode = body;
body = http.STATUS_CODES[body];
break;
// string defaulting to html
case 'string':
if (!this.get('Content-Type')) {
Expand All @@ -118,6 +120,7 @@ res.send = function(body){
}
break;
case 'boolean':
case 'number':
case 'object':
if (null == body) {
body = '';
Expand Down
15 changes: 15 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ describe('res', function(){
})
})

describe('.send(code, number)', function(){
it('should send number as json', function(done){
var app = express();

app.use(function(req, res){
res.send(200, 0.123);
});

request(app)
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(200, '0.123', done);
})
})

describe('.send(String)', function(){
it('should send as html', function(done){
var app = express();
Expand Down

0 comments on commit 544c666

Please sign in to comment.