ocd
This commit is contained in:
parent
d6c523d107
commit
9e8d6a3aa0
7 changed files with 20 additions and 20 deletions
|
@ -13,7 +13,7 @@
|
||||||
`X-Response-Time` header field the middleware would look like the following:
|
`X-Response-Time` header field the middleware would look like the following:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function *responseTime(next){
|
function *responseTime(next) {
|
||||||
var start = new Date;
|
var start = new Date;
|
||||||
yield next;
|
yield next;
|
||||||
var ms = new Date - start;
|
var ms = new Date - start;
|
||||||
|
@ -88,7 +88,7 @@ app.use(function *response(){
|
||||||
and returns the middleware itself:
|
and returns the middleware itself:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function logger(format){
|
function logger(format) {
|
||||||
format = format || ':method ":url"';
|
format = format || ':method ":url"';
|
||||||
|
|
||||||
return function *(next){
|
return function *(next){
|
||||||
|
@ -111,7 +111,7 @@ app.use(logger(':method :url'));
|
||||||
Naming middleware is optional, however it's useful for debugging purposes to assign a name.
|
Naming middleware is optional, however it's useful for debugging purposes to assign a name.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function logger(format){
|
function logger(format) {
|
||||||
return function *logger(next){
|
return function *logger(next){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ function logger(format){
|
||||||
Sometimes you want to "compose" multiple middleware into a single middleware for easy re-use or exporting. To do so, you may chain them together with `.call(this, next)`s, then return another function that yields the chain.
|
Sometimes you want to "compose" multiple middleware into a single middleware for easy re-use or exporting. To do so, you may chain them together with `.call(this, next)`s, then return another function that yields the chain.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function *random(next){
|
function *random(next) {
|
||||||
if ('/random' == this.path) {
|
if ('/random' == this.path) {
|
||||||
this.body = Math.floor(Math.random()*10);
|
this.body = Math.floor(Math.random()*10);
|
||||||
} else {
|
} else {
|
||||||
|
@ -139,7 +139,7 @@ function *backwords(next) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function *pi(next){
|
function *pi(next) {
|
||||||
if ('/pi' == this.path) {
|
if ('/pi' == this.path) {
|
||||||
this.body = String(Math.PI);
|
this.body = String(Math.PI);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -162,7 +162,7 @@ app.onerror = function(err){
|
||||||
* Response middleware.
|
* Response middleware.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function *respond(next){
|
function *respond(next) {
|
||||||
if (this.app.poweredBy) this.set('X-Powered-By', 'koa');
|
if (this.app.poweredBy) this.set('X-Powered-By', 'koa');
|
||||||
|
|
||||||
yield *next;
|
yield *next;
|
||||||
|
@ -217,4 +217,4 @@ function *respond(next){
|
||||||
this.length = Buffer.byteLength(body);
|
this.length = Buffer.byteLength(body);
|
||||||
if (head) return res.end();
|
if (head) return res.end();
|
||||||
res.end(body);
|
res.end(body);
|
||||||
}
|
}
|
||||||
|
|
|
@ -486,7 +486,7 @@ module.exports = {
|
||||||
* @api public
|
* @api public
|
||||||
*/
|
*/
|
||||||
|
|
||||||
acceptsLanguages: function() {
|
acceptsLanguages: function(){
|
||||||
return this.accept.languages.apply(this.accept, arguments);
|
return this.accept.languages.apply(this.accept, arguments);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -257,7 +257,7 @@ module.exports = {
|
||||||
* @api public
|
* @api public
|
||||||
*/
|
*/
|
||||||
|
|
||||||
set type(type){
|
set type(type) {
|
||||||
if (!~type.indexOf('/')) {
|
if (!~type.indexOf('/')) {
|
||||||
type = mime.lookup(type);
|
type = mime.lookup(type);
|
||||||
var cs = mime.charsets.lookup(type);
|
var cs = mime.charsets.lookup(type);
|
||||||
|
|
|
@ -33,7 +33,7 @@ describe('app', function(){
|
||||||
this.status = 204;
|
this.status = 204;
|
||||||
// throw if .writeHead or .end is called
|
// throw if .writeHead or .end is called
|
||||||
this.res.writeHead =
|
this.res.writeHead =
|
||||||
this.res.end = function () {
|
this.res.end = function(){
|
||||||
throw new Error('response sent');
|
throw new Error('response sent');
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
@ -125,7 +125,7 @@ describe('app.respond', function(){
|
||||||
this.respond = false;
|
this.respond = false;
|
||||||
|
|
||||||
var res = this.res;
|
var res = this.res;
|
||||||
setImmediate(function () {
|
setImmediate(function(){
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
res.end('lol');
|
res.end('lol');
|
||||||
})
|
})
|
||||||
|
@ -229,14 +229,14 @@ describe('app.respond', function(){
|
||||||
res.setHeader("Content-Type", "text/html")
|
res.setHeader("Content-Type", "text/html")
|
||||||
res.status = 200;
|
res.status = 200;
|
||||||
res.write('Hello');
|
res.write('Hello');
|
||||||
setTimeout(function () {
|
setTimeout(function(){
|
||||||
res.end("Goodbye")
|
res.end("Goodbye")
|
||||||
}, 0);
|
}, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
var errorCaught = false;
|
var errorCaught = false;
|
||||||
|
|
||||||
app.on('error', function (err) {
|
app.on('error', function(err){
|
||||||
errorCaught = err;
|
errorCaught = err;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ describe('app.respond', function(){
|
||||||
res.setHeader("Content-Type", "text/html")
|
res.setHeader("Content-Type", "text/html")
|
||||||
res.status = 200;
|
res.status = 200;
|
||||||
res.write('Hello');
|
res.write('Hello');
|
||||||
setTimeout(function () {
|
setTimeout(function(){
|
||||||
res.end("Goodbye")
|
res.end("Goodbye")
|
||||||
}, 0);
|
}, 0);
|
||||||
});
|
});
|
||||||
|
@ -306,7 +306,7 @@ describe('app.respond', function(){
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(204)
|
.expect(204)
|
||||||
.expect('')
|
.expect('')
|
||||||
.end(function (err, res) {
|
.end(function(err, res){
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
||||||
res.header.should.not.have.property('content-type');
|
res.header.should.not.have.property('content-type');
|
||||||
|
@ -329,7 +329,7 @@ describe('app.respond', function(){
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(205)
|
.expect(205)
|
||||||
.expect('')
|
.expect('')
|
||||||
.end(function (err, res) {
|
.end(function(err, res){
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
||||||
res.header.should.not.have.property('content-type');
|
res.header.should.not.have.property('content-type');
|
||||||
|
@ -352,7 +352,7 @@ describe('app.respond', function(){
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(304)
|
.expect(304)
|
||||||
.expect('')
|
.expect('')
|
||||||
.end(function (err, res) {
|
.end(function(err, res){
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
||||||
res.header.should.not.have.property('content-type');
|
res.header.should.not.have.property('content-type');
|
||||||
|
@ -629,4 +629,4 @@ describe('app.response', function(){
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(204, done);
|
.expect(204, done);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -99,4 +99,4 @@ function escape(html) {
|
||||||
.replace(/"/g, '"')
|
.replace(/"/g, '"')
|
||||||
.replace(/</g, '<')
|
.replace(/</g, '<')
|
||||||
.replace(/>/g, '>');
|
.replace(/>/g, '>');
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,4 +65,4 @@ describe('res.status=', function(){
|
||||||
describe('when 304', function(){
|
describe('when 304', function(){
|
||||||
strip(304);
|
strip(304);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue