refactor: use statuses
This commit is contained in:
parent
9b1651a8e6
commit
182f9d6fa4
5 changed files with 16 additions and 56 deletions
|
@ -11,6 +11,7 @@ var request = require('./request');
|
||||||
var response = require('./response');
|
var response = require('./response');
|
||||||
var Cookies = require('cookies');
|
var Cookies = require('cookies');
|
||||||
var accepts = require('accepts');
|
var accepts = require('accepts');
|
||||||
|
var status = require('statuses');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var only = require('only');
|
var only = require('only');
|
||||||
|
@ -172,17 +173,16 @@ function *respond(next){
|
||||||
if (res.headersSent || !this.writable) return;
|
if (res.headersSent || !this.writable) return;
|
||||||
|
|
||||||
var body = this.body;
|
var body = this.body;
|
||||||
var status = this.status = this.status || 404;
|
var code = this.status = this.status || 404;
|
||||||
var head = 'HEAD' == this.method;
|
var head = 'HEAD' == this.method;
|
||||||
var noContent = ~[204, 205, 304].indexOf(status);
|
|
||||||
|
|
||||||
// ignore body
|
// ignore body
|
||||||
if (noContent) return res.end();
|
if (status.empty[code]) return res.end();
|
||||||
|
|
||||||
// status body
|
// status body
|
||||||
if (null == body) {
|
if (null == body) {
|
||||||
this.type = 'text';
|
this.type = 'text';
|
||||||
body = http.STATUS_CODES[status];
|
body = status[code];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffer body
|
// Buffer body
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var debug = require('debug')('koa:response');
|
var debug = require('debug')('koa:response');
|
||||||
var statuses = require('./status');
|
var status = require('statuses');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var mime = require('mime');
|
var mime = require('mime');
|
||||||
|
@ -66,19 +66,14 @@ module.exports = {
|
||||||
/**
|
/**
|
||||||
* Set response status code.
|
* Set response status code.
|
||||||
*
|
*
|
||||||
* @param {Number|String} val
|
* @param {Number|String} code
|
||||||
* @api public
|
* @api public
|
||||||
*/
|
*/
|
||||||
|
|
||||||
set status(val) {
|
set status(code) {
|
||||||
if ('string' == typeof val) {
|
if ('number' != typeof code) code = status(code);
|
||||||
var n = statuses[val.toLowerCase()];
|
this._status = this.res.statusCode = code;
|
||||||
if (!n) throw new Error(statusError(val));
|
if (this.body && status.empty[code]) this.body = null;
|
||||||
val = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._status = this.res.statusCode = val;
|
|
||||||
if (this.body && ~[204, 205, 304].indexOf(val)) this.body = null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -217,7 +212,7 @@ module.exports = {
|
||||||
redirect: function(url, alt){
|
redirect: function(url, alt){
|
||||||
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
|
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
|
||||||
this.set('Location', url);
|
this.set('Location', url);
|
||||||
if (!~[300, 301, 302, 303, 305, 307].indexOf(this.status)) this.status = 302;
|
if (!status.redirect[this.status]) this.status = 302;
|
||||||
|
|
||||||
// html
|
// html
|
||||||
if (this.ctx.accepts('html')) {
|
if (this.ctx.accepts('html')) {
|
||||||
|
@ -454,25 +449,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Return status error message.
|
|
||||||
*
|
|
||||||
* @param {String} val
|
|
||||||
* @return {String}
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function statusError(val) {
|
|
||||||
var s = 'invalid status string "' + val + '", try:\n\n';
|
|
||||||
|
|
||||||
Object.keys(statuses).forEach(function(name){
|
|
||||||
var n = statuses[name];
|
|
||||||
s += ' - ' + n + ' "' + name + '"\n';
|
|
||||||
});
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escape special characters in the given string of html.
|
* Escape special characters in the given string of html.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var http = require('http');
|
|
||||||
var codes = http.STATUS_CODES;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Produce exports[STATUS] = CODE map.
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.keys(codes).forEach(function(code){
|
|
||||||
var n = ~~code;
|
|
||||||
var s = codes[n].toLowerCase();
|
|
||||||
exports[s] = n;
|
|
||||||
});
|
|
|
@ -21,6 +21,7 @@
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"statuses": "~1.0.1",
|
||||||
"accepts": "~1.0.0",
|
"accepts": "~1.0.0",
|
||||||
"type-is": "~1.0.0",
|
"type-is": "~1.0.0",
|
||||||
"finished": "~1.1.1",
|
"finished": "~1.1.1",
|
||||||
|
|
|
@ -9,14 +9,14 @@ describe('ctx.onerror(err)', function(){
|
||||||
app.use(function *(next){
|
app.use(function *(next){
|
||||||
this.body = 'something else';
|
this.body = 'something else';
|
||||||
|
|
||||||
this.throw(499, 'boom');
|
this.throw(418, 'boom');
|
||||||
})
|
})
|
||||||
|
|
||||||
var server = app.listen();
|
var server = app.listen();
|
||||||
|
|
||||||
request(server)
|
request(server)
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(499)
|
.expect(418)
|
||||||
.expect('Content-Type', 'text/plain; charset=utf-8')
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
||||||
.expect('Content-Length', '4')
|
.expect('Content-Length', '4')
|
||||||
.end(done);
|
.end(done);
|
||||||
|
@ -30,14 +30,14 @@ describe('ctx.onerror(err)', function(){
|
||||||
this.set('X-CSRF-Token', 'asdf');
|
this.set('X-CSRF-Token', 'asdf');
|
||||||
this.body = 'response';
|
this.body = 'response';
|
||||||
|
|
||||||
this.throw(499, 'boom');
|
this.throw(418, 'boom');
|
||||||
})
|
})
|
||||||
|
|
||||||
var server = app.listen();
|
var server = app.listen();
|
||||||
|
|
||||||
request(server)
|
request(server)
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(499)
|
.expect(418)
|
||||||
.expect('Content-Type', 'text/plain; charset=utf-8')
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
||||||
.expect('Content-Length', '4')
|
.expect('Content-Length', '4')
|
||||||
.end(function(err, res){
|
.end(function(err, res){
|
||||||
|
|
Loading…
Reference in a new issue