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:
|
||||
|
||||
```js
|
||||
function *responseTime(next){
|
||||
function *responseTime(next) {
|
||||
var start = new Date;
|
||||
yield next;
|
||||
var ms = new Date - start;
|
||||
|
@ -88,7 +88,7 @@ app.use(function *response(){
|
|||
and returns the middleware itself:
|
||||
|
||||
```js
|
||||
function logger(format){
|
||||
function logger(format) {
|
||||
format = format || ':method ":url"';
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
function logger(format){
|
||||
function logger(format) {
|
||||
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.
|
||||
|
||||
```js
|
||||
function *random(next){
|
||||
function *random(next) {
|
||||
if ('/random' == this.path) {
|
||||
this.body = Math.floor(Math.random()*10);
|
||||
} else {
|
||||
|
@ -139,7 +139,7 @@ function *backwords(next) {
|
|||
}
|
||||
}
|
||||
|
||||
function *pi(next){
|
||||
function *pi(next) {
|
||||
if ('/pi' == this.path) {
|
||||
this.body = String(Math.PI);
|
||||
} else {
|
||||
|
|
|
@ -162,7 +162,7 @@ app.onerror = function(err){
|
|||
* Response middleware.
|
||||
*/
|
||||
|
||||
function *respond(next){
|
||||
function *respond(next) {
|
||||
if (this.app.poweredBy) this.set('X-Powered-By', 'koa');
|
||||
|
||||
yield *next;
|
||||
|
@ -217,4 +217,4 @@ function *respond(next){
|
|||
this.length = Buffer.byteLength(body);
|
||||
if (head) return res.end();
|
||||
res.end(body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -486,7 +486,7 @@ module.exports = {
|
|||
* @api public
|
||||
*/
|
||||
|
||||
acceptsLanguages: function() {
|
||||
acceptsLanguages: function(){
|
||||
return this.accept.languages.apply(this.accept, arguments);
|
||||
},
|
||||
|
||||
|
|
|
@ -257,7 +257,7 @@ module.exports = {
|
|||
* @api public
|
||||
*/
|
||||
|
||||
set type(type){
|
||||
set type(type) {
|
||||
if (!~type.indexOf('/')) {
|
||||
type = mime.lookup(type);
|
||||
var cs = mime.charsets.lookup(type);
|
||||
|
|
|
@ -33,7 +33,7 @@ describe('app', function(){
|
|||
this.status = 204;
|
||||
// throw if .writeHead or .end is called
|
||||
this.res.writeHead =
|
||||
this.res.end = function () {
|
||||
this.res.end = function(){
|
||||
throw new Error('response sent');
|
||||
};
|
||||
})
|
||||
|
@ -125,7 +125,7 @@ describe('app.respond', function(){
|
|||
this.respond = false;
|
||||
|
||||
var res = this.res;
|
||||
setImmediate(function () {
|
||||
setImmediate(function(){
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
res.end('lol');
|
||||
})
|
||||
|
@ -229,14 +229,14 @@ describe('app.respond', function(){
|
|||
res.setHeader("Content-Type", "text/html")
|
||||
res.status = 200;
|
||||
res.write('Hello');
|
||||
setTimeout(function () {
|
||||
setTimeout(function(){
|
||||
res.end("Goodbye")
|
||||
}, 0);
|
||||
});
|
||||
|
||||
var errorCaught = false;
|
||||
|
||||
app.on('error', function (err) {
|
||||
app.on('error', function(err){
|
||||
errorCaught = err;
|
||||
});
|
||||
|
||||
|
@ -260,7 +260,7 @@ describe('app.respond', function(){
|
|||
res.setHeader("Content-Type", "text/html")
|
||||
res.status = 200;
|
||||
res.write('Hello');
|
||||
setTimeout(function () {
|
||||
setTimeout(function(){
|
||||
res.end("Goodbye")
|
||||
}, 0);
|
||||
});
|
||||
|
@ -306,7 +306,7 @@ describe('app.respond', function(){
|
|||
.get('/')
|
||||
.expect(204)
|
||||
.expect('')
|
||||
.end(function (err, res) {
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
|
||||
res.header.should.not.have.property('content-type');
|
||||
|
@ -329,7 +329,7 @@ describe('app.respond', function(){
|
|||
.get('/')
|
||||
.expect(205)
|
||||
.expect('')
|
||||
.end(function (err, res) {
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
|
||||
res.header.should.not.have.property('content-type');
|
||||
|
@ -352,7 +352,7 @@ describe('app.respond', function(){
|
|||
.get('/')
|
||||
.expect(304)
|
||||
.expect('')
|
||||
.end(function (err, res) {
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
|
||||
res.header.should.not.have.property('content-type');
|
||||
|
@ -629,4 +629,4 @@ describe('app.response', function(){
|
|||
.get('/')
|
||||
.expect(204, done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -99,4 +99,4 @@ function escape(html) {
|
|||
.replace(/"/g, '"')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,4 +65,4 @@ describe('res.status=', function(){
|
|||
describe('when 304', function(){
|
||||
strip(304);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue