Skip to content

Commit

Permalink
Add res.sendStatus
Browse files Browse the repository at this point in the history
closes #2269
closes #2297
closes #2340
  • Loading branch information
seth-admittedly authored and dougwilson committed Sep 9, 2014
1 parent 51d33ed commit 12f92a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions History.md
@@ -1,6 +1,7 @@
unreleased
==========

* Add `res.sendStatus`
* Invoke callback for sendfile when client aborts
- Applies to `res.sendFile`, `res.sendfile`, and `res.download`
- `err` will be populated with request aborted error
Expand Down
24 changes: 24 additions & 0 deletions lib/response.js
Expand Up @@ -303,6 +303,30 @@ res.jsonp = function jsonp(obj) {
return this.send(body);
};

/**
* Send given HTTP status code.
*
* Sets the response status to `statusCode` and the body of the
* response to the standard description from node's http.STATUS_CODES
* or the statusCode number if no description.
*
* Examples:
*
* res.sendStatus(200);
*
* @param {number} statusCode
* @api public
*/

res.sendStatus = function sendStatus(statusCode) {
var body = http.STATUS_CODES[statusCode] || String(statusCode);

this.statusCode = statusCode;
this.type('txt');

return this.send(body);
};

/**
* Transfer the file at the given `path`.
*
Expand Down
32 changes: 32 additions & 0 deletions test/res.sendStatus.js
@@ -0,0 +1,32 @@

var assert = require('assert')
var express = require('..')
var request = require('supertest')

describe('res', function () {
describe('.sendStatus(statusCode)', function () {
it('should send the status code and message as body', function (done) {
var app = express();

app.use(function(req, res){
res.sendStatus(201);
});

request(app)
.get('/')
.expect(201, 'Created', done);
})

it('should work with unknown code', function (done) {
var app = express();

app.use(function(req, res){
res.sendStatus(599);
});

request(app)
.get('/')
.expect(599, '599', done);
})
})
})

0 comments on commit 12f92a5

Please sign in to comment.