Add res.sendStatus

closes #2269
closes #2297
closes #2340
This commit is contained in:
Seth Samuel 2014-08-13 09:09:59 -04:00 committed by Douglas Christopher Wilson
parent 51d33edb79
commit 12f92a50dc
3 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -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`.
*

32
test/res.sendStatus.js Normal file
View File

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