commit
95a5bcd3e0
5 changed files with 33 additions and 33 deletions
|
@ -99,7 +99,7 @@ this.throw('something exploded');
|
||||||
For example `this.throw('name required', 400)` is equivalent to:
|
For example `this.throw('name required', 400)` is equivalent to:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var err = new Error('name required');
|
const err = new Error('name required');
|
||||||
err.status = 400;
|
err.status = 400;
|
||||||
throw err;
|
throw err;
|
||||||
```
|
```
|
||||||
|
|
|
@ -29,8 +29,8 @@ $ node my-koa-app.js
|
||||||
The obligatory hello world application:
|
The obligatory hello world application:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var koa = require('koa');
|
const koa = require('koa');
|
||||||
var app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
app.use(function *(){
|
app.use(function *(){
|
||||||
this.body = 'Hello World';
|
this.body = 'Hello World';
|
||||||
|
@ -55,24 +55,24 @@ app.listen(3000);
|
||||||
its upstream behaviour.
|
its upstream behaviour.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var koa = require('koa');
|
const koa = require('koa');
|
||||||
var app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
// x-response-time
|
// x-response-time
|
||||||
|
|
||||||
app.use(function *(next){
|
app.use(function *(next){
|
||||||
var start = new Date;
|
const start = new Date;
|
||||||
yield next;
|
yield next;
|
||||||
var ms = new Date - start;
|
const ms = new Date - start;
|
||||||
this.set('X-Response-Time', ms + 'ms');
|
this.set('X-Response-Time', ms + 'ms');
|
||||||
});
|
});
|
||||||
|
|
||||||
// logger
|
// logger
|
||||||
|
|
||||||
app.use(function *(next){
|
app.use(function *(next){
|
||||||
var start = new Date;
|
const start = new Date;
|
||||||
yield next;
|
yield next;
|
||||||
var ms = new Date - start;
|
const ms = new Date - start;
|
||||||
console.log('%s %s - %s', this.method, this.url, ms);
|
console.log('%s %s - %s', this.method, this.url, ms);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -105,17 +105,17 @@ app.listen(3000);
|
||||||
`Server#listen()`. These arguments are documented on [nodejs.org](http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback). The following is a useless Koa application bound to port `3000`:
|
`Server#listen()`. These arguments are documented on [nodejs.org](http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback). The following is a useless Koa application bound to port `3000`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var koa = require('koa');
|
const koa = require('koa');
|
||||||
var app = new Koa();
|
const app = new Koa();
|
||||||
app.listen(3000);
|
app.listen(3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
The `app.listen(...)` method is simply sugar for the following:
|
The `app.listen(...)` method is simply sugar for the following:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var http = require('http');
|
const http = require('http');
|
||||||
var koa = require('koa');
|
const koa = require('koa');
|
||||||
var app = new Koa();
|
const app = new Koa();
|
||||||
http.createServer(app.callback()).listen(3000);
|
http.createServer(app.callback()).listen(3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -123,9 +123,9 @@ http.createServer(app.callback()).listen(3000);
|
||||||
or on multiple addresses:
|
or on multiple addresses:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var http = require('http');
|
const http = require('http');
|
||||||
var koa = require('koa');
|
const koa = require('koa');
|
||||||
var app = new Koa();
|
const app = new Koa();
|
||||||
http.createServer(app.callback()).listen(3000);
|
http.createServer(app.callback()).listen(3000);
|
||||||
http.createServer(app.callback()).listen(3001);
|
http.createServer(app.callback()).listen(3001);
|
||||||
```
|
```
|
||||||
|
|
|
@ -96,7 +96,7 @@ this.request.href
|
||||||
Get request `Content-Type` void of parameters such as "charset".
|
Get request `Content-Type` void of parameters such as "charset".
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var ct = this.request.type;
|
const ct = this.request.type;
|
||||||
// => "image/png"
|
// => "image/png"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,7 @@ If `response.status` has not been set, Koa will automatically set the status to
|
||||||
Get a response header field value with case-insensitive `field`.
|
Get a response header field value with case-insensitive `field`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var etag = this.get('ETag');
|
const etag = this.get('ETag');
|
||||||
```
|
```
|
||||||
|
|
||||||
### response.set(field, value)
|
### response.set(field, value)
|
||||||
|
@ -183,7 +183,7 @@ this.set({
|
||||||
Get response `Content-Type` void of parameters such as "charset".
|
Get response `Content-Type` void of parameters such as "charset".
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var ct = this.type;
|
const ct = this.type;
|
||||||
// => "image/png"
|
// => "image/png"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -214,14 +214,14 @@ this.type = 'png';
|
||||||
all HTML responses except for streams.
|
all HTML responses except for streams.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var minify = require('html-minifier');
|
const minify = require('html-minifier');
|
||||||
|
|
||||||
app.use(function *minifyHTML(next){
|
app.use(function *minifyHTML(next){
|
||||||
yield next;
|
yield next;
|
||||||
|
|
||||||
if (!this.response.is('html')) return;
|
if (!this.response.is('html')) return;
|
||||||
|
|
||||||
var body = this.body;
|
const body = this.body;
|
||||||
if (!body || body.pipe) return;
|
if (!body || body.pipe) return;
|
||||||
|
|
||||||
if (Buffer.isBuffer(body)) body = body.toString();
|
if (Buffer.isBuffer(body)) body = body.toString();
|
||||||
|
|
|
@ -14,9 +14,9 @@
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function *responseTime(next) {
|
function *responseTime(next) {
|
||||||
var start = new Date;
|
const start = new Date;
|
||||||
yield next;
|
yield next;
|
||||||
var ms = new Date - start;
|
const ms = new Date - start;
|
||||||
this.set('X-Response-Time', ms + 'ms');
|
this.set('X-Response-Time', ms + 'ms');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,9 +27,9 @@ app.use(responseTime);
|
||||||
|
|
||||||
```js
|
```js
|
||||||
app.use(function *(next){
|
app.use(function *(next){
|
||||||
var start = new Date;
|
const start = new Date;
|
||||||
yield next;
|
yield next;
|
||||||
var ms = new Date - start;
|
const ms = new Date - start;
|
||||||
this.set('X-Response-Time', ms + 'ms');
|
this.set('X-Response-Time', ms + 'ms');
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -92,7 +92,7 @@ function logger(format) {
|
||||||
format = format || ':method ":url"';
|
format = format || ':method ":url"';
|
||||||
|
|
||||||
return function *(next){
|
return function *(next){
|
||||||
var str = format
|
const str = format
|
||||||
.replace(':method', this.method)
|
.replace(':method', this.method)
|
||||||
.replace(':url', this.url);
|
.replace(':url', this.url);
|
||||||
|
|
||||||
|
@ -218,12 +218,12 @@ app.use(function *(next){
|
||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var fs = require('co-fs');
|
const fs = require('co-fs');
|
||||||
|
|
||||||
app.use(function *(){
|
app.use(function *(){
|
||||||
var paths = yield fs.readdir('docs');
|
const paths = yield fs.readdir('docs');
|
||||||
|
|
||||||
var files = yield paths.map(function(path){
|
const files = yield paths.map(function(path){
|
||||||
return fs.readFile('docs/' + path, 'utf8');
|
return fs.readFile('docs/' + path, 'utf8');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -255,10 +255,10 @@ $ DEBUG=koa* node --harmony examples/simple
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
var static = require('koa-static');
|
const static = require('koa-static');
|
||||||
|
|
||||||
var publicFiles = static(path.join(__dirname, 'public'));
|
const publicFiles = static(path.join(__dirname, 'public'));
|
||||||
publicFiles._name = 'static /public';
|
publicFiles._name = 'static /public';
|
||||||
|
|
||||||
app.use(publicFiles);
|
app.use(publicFiles);
|
||||||
|
|
Loading…
Reference in a new issue