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:
|
||||
|
||||
```js
|
||||
var err = new Error('name required');
|
||||
const err = new Error('name required');
|
||||
err.status = 400;
|
||||
throw err;
|
||||
```
|
||||
|
|
|
@ -29,8 +29,8 @@ $ node my-koa-app.js
|
|||
The obligatory hello world application:
|
||||
|
||||
```js
|
||||
var koa = require('koa');
|
||||
var app = new Koa();
|
||||
const koa = require('koa');
|
||||
const app = new Koa();
|
||||
|
||||
app.use(function *(){
|
||||
this.body = 'Hello World';
|
||||
|
@ -55,24 +55,24 @@ app.listen(3000);
|
|||
its upstream behaviour.
|
||||
|
||||
```js
|
||||
var koa = require('koa');
|
||||
var app = new Koa();
|
||||
const koa = require('koa');
|
||||
const app = new Koa();
|
||||
|
||||
// x-response-time
|
||||
|
||||
app.use(function *(next){
|
||||
var start = new Date;
|
||||
const start = new Date;
|
||||
yield next;
|
||||
var ms = new Date - start;
|
||||
const ms = new Date - start;
|
||||
this.set('X-Response-Time', ms + 'ms');
|
||||
});
|
||||
|
||||
// logger
|
||||
|
||||
app.use(function *(next){
|
||||
var start = new Date;
|
||||
const start = new Date;
|
||||
yield next;
|
||||
var ms = new Date - start;
|
||||
const ms = new Date - start;
|
||||
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`:
|
||||
|
||||
```js
|
||||
var koa = require('koa');
|
||||
var app = new Koa();
|
||||
const koa = require('koa');
|
||||
const app = new Koa();
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
The `app.listen(...)` method is simply sugar for the following:
|
||||
|
||||
```js
|
||||
var http = require('http');
|
||||
var koa = require('koa');
|
||||
var app = new Koa();
|
||||
const http = require('http');
|
||||
const koa = require('koa');
|
||||
const app = new Koa();
|
||||
http.createServer(app.callback()).listen(3000);
|
||||
```
|
||||
|
||||
|
@ -123,9 +123,9 @@ http.createServer(app.callback()).listen(3000);
|
|||
or on multiple addresses:
|
||||
|
||||
```js
|
||||
var http = require('http');
|
||||
var koa = require('koa');
|
||||
var app = new Koa();
|
||||
const http = require('http');
|
||||
const koa = require('koa');
|
||||
const app = new Koa();
|
||||
http.createServer(app.callback()).listen(3000);
|
||||
http.createServer(app.callback()).listen(3001);
|
||||
```
|
||||
|
|
|
@ -96,7 +96,7 @@ this.request.href
|
|||
Get request `Content-Type` void of parameters such as "charset".
|
||||
|
||||
```js
|
||||
var ct = this.request.type;
|
||||
const ct = this.request.type;
|
||||
// => "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`.
|
||||
|
||||
```js
|
||||
var etag = this.get('ETag');
|
||||
const etag = this.get('ETag');
|
||||
```
|
||||
|
||||
### response.set(field, value)
|
||||
|
@ -183,7 +183,7 @@ this.set({
|
|||
Get response `Content-Type` void of parameters such as "charset".
|
||||
|
||||
```js
|
||||
var ct = this.type;
|
||||
const ct = this.type;
|
||||
// => "image/png"
|
||||
```
|
||||
|
||||
|
@ -214,14 +214,14 @@ this.type = 'png';
|
|||
all HTML responses except for streams.
|
||||
|
||||
```js
|
||||
var minify = require('html-minifier');
|
||||
const minify = require('html-minifier');
|
||||
|
||||
app.use(function *minifyHTML(next){
|
||||
yield next;
|
||||
|
||||
if (!this.response.is('html')) return;
|
||||
|
||||
var body = this.body;
|
||||
const body = this.body;
|
||||
if (!body || body.pipe) return;
|
||||
|
||||
if (Buffer.isBuffer(body)) body = body.toString();
|
||||
|
|
|
@ -14,9 +14,9 @@
|
|||
|
||||
```js
|
||||
function *responseTime(next) {
|
||||
var start = new Date;
|
||||
const start = new Date;
|
||||
yield next;
|
||||
var ms = new Date - start;
|
||||
const ms = new Date - start;
|
||||
this.set('X-Response-Time', ms + 'ms');
|
||||
}
|
||||
|
||||
|
@ -27,9 +27,9 @@ app.use(responseTime);
|
|||
|
||||
```js
|
||||
app.use(function *(next){
|
||||
var start = new Date;
|
||||
const start = new Date;
|
||||
yield next;
|
||||
var ms = new Date - start;
|
||||
const ms = new Date - start;
|
||||
this.set('X-Response-Time', ms + 'ms');
|
||||
});
|
||||
```
|
||||
|
@ -92,7 +92,7 @@ function logger(format) {
|
|||
format = format || ':method ":url"';
|
||||
|
||||
return function *(next){
|
||||
var str = format
|
||||
const str = format
|
||||
.replace(':method', this.method)
|
||||
.replace(':url', this.url);
|
||||
|
||||
|
@ -218,12 +218,12 @@ app.use(function *(next){
|
|||
|
||||
|
||||
```js
|
||||
var fs = require('co-fs');
|
||||
const fs = require('co-fs');
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
|
@ -255,10 +255,10 @@ $ DEBUG=koa* node --harmony examples/simple
|
|||
For example:
|
||||
|
||||
```js
|
||||
var path = require('path');
|
||||
var static = require('koa-static');
|
||||
const path = require('path');
|
||||
const static = require('koa-static');
|
||||
|
||||
var publicFiles = static(path.join(__dirname, 'public'));
|
||||
const publicFiles = static(path.join(__dirname, 'public'));
|
||||
publicFiles._name = 'static /public';
|
||||
|
||||
app.use(publicFiles);
|
||||
|
|
Loading…
Reference in a new issue