docs: improve consistency in api code examples (#1029)
This commit is contained in:
parent
0c44c11ce3
commit
40a6dd2c80
4 changed files with 17 additions and 17 deletions
|
@ -12,7 +12,7 @@
|
||||||
snippet:
|
snippet:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
app.use(async (ctx, next) => {
|
app.use(async ctx => {
|
||||||
ctx; // is the Context
|
ctx; // is the Context
|
||||||
ctx.request; // is a koa Request
|
ctx.request; // is a koa Request
|
||||||
ctx.response; // is a koa Response
|
ctx.response; // is a koa Response
|
||||||
|
|
|
@ -52,7 +52,7 @@ You can also use the [env preset](http://babeljs.io/docs/plugins/preset-env/) wi
|
||||||
const Koa = require('koa');
|
const Koa = require('koa');
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
app.use(ctx => {
|
app.use(async ctx => {
|
||||||
ctx.body = 'Hello World';
|
ctx.body = 'Hello World';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ const app = new Koa();
|
||||||
|
|
||||||
// x-response-time
|
// x-response-time
|
||||||
|
|
||||||
app.use(async function (ctx, next) {
|
app.use(async (ctx, next) => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await next();
|
await next();
|
||||||
const ms = Date.now() - start;
|
const ms = Date.now() - start;
|
||||||
|
@ -89,7 +89,7 @@ app.use(async function (ctx, next) {
|
||||||
|
|
||||||
// logger
|
// logger
|
||||||
|
|
||||||
app.use(async function (ctx, next) {
|
app.use(async (ctx, next) => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await next();
|
await next();
|
||||||
const ms = Date.now() - start;
|
const ms = Date.now() - start;
|
||||||
|
@ -98,7 +98,7 @@ app.use(async function (ctx, next) {
|
||||||
|
|
||||||
// response
|
// response
|
||||||
|
|
||||||
app.use(ctx => {
|
app.use(async ctx => {
|
||||||
ctx.body = 'Hello World';
|
ctx.body = 'Hello World';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ ctx.cookies.set('name', 'tobi', { signed: true });
|
||||||
```js
|
```js
|
||||||
app.context.db = db();
|
app.context.db = db();
|
||||||
|
|
||||||
app.use(async (ctx) => {
|
app.use(async ctx => {
|
||||||
console.log(ctx.db);
|
console.log(ctx.db);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -212,17 +212,17 @@ Note:
|
||||||
To perform custom error-handling logic such as centralized logging you can add an "error" event listener:
|
To perform custom error-handling logic such as centralized logging you can add an "error" event listener:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
app.on('error', err =>
|
app.on('error', err => {
|
||||||
log.error('server error', err)
|
log.error('server error', err)
|
||||||
);
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
If an error is in the req/res cycle and it is _not_ possible to respond to the client, the `Context` instance is also passed:
|
If an error is in the req/res cycle and it is _not_ possible to respond to the client, the `Context` instance is also passed:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
app.on('error', (err, ctx) =>
|
app.on('error', (err, ctx) => {
|
||||||
log.error('server error', err, ctx)
|
log.error('server error', err, ctx)
|
||||||
);
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
When an error occurs _and_ it is still possible to respond to the client, aka no data has been written to the socket, Koa will respond
|
When an error occurs _and_ it is still possible to respond to the client, aka no data has been written to the socket, Koa will respond
|
||||||
|
|
|
@ -61,7 +61,7 @@ ctx.request.origin
|
||||||
Get full request URL, include `protocol`, `host` and `url`.
|
Get full request URL, include `protocol`, `host` and `url`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
ctx.request.href
|
ctx.request.href;
|
||||||
// => http://example.com/foo/bar?q=1
|
// => http://example.com/foo/bar?q=1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ ctx.request.href
|
||||||
Get request `Content-Type` void of parameters such as "charset".
|
Get request `Content-Type` void of parameters such as "charset".
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const ct = ctx.request.type
|
const ct = ctx.request.type;
|
||||||
// => "image/png"
|
// => "image/png"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ const ct = ctx.request.type
|
||||||
Get request charset when present, or `undefined`:
|
Get request charset when present, or `undefined`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
ctx.request.charset
|
ctx.request.charset;
|
||||||
// => "utf-8"
|
// => "utf-8"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ ctx.request.charset
|
||||||
setter does _not_ support nested objects.
|
setter does _not_ support nested objects.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
ctx.query = { next: '/login' }
|
ctx.query = { next: '/login' };
|
||||||
```
|
```
|
||||||
|
|
||||||
### request.fresh
|
### request.fresh
|
||||||
|
|
|
@ -150,7 +150,7 @@ If `response.status` has not been set, Koa will automatically set the status to
|
||||||
```js
|
```js
|
||||||
const PassThrough = require('stream').PassThrough;
|
const PassThrough = require('stream').PassThrough;
|
||||||
|
|
||||||
app.use(function * (next) {
|
app.use(async ctx => {
|
||||||
ctx.body = someHTTPStream.on('error', ctx.onerror).pipe(PassThrough());
|
ctx.body = someHTTPStream.on('error', ctx.onerror).pipe(PassThrough());
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -234,8 +234,8 @@ ctx.type = 'png';
|
||||||
```js
|
```js
|
||||||
const minify = require('html-minifier');
|
const minify = require('html-minifier');
|
||||||
|
|
||||||
app.use(function * minifyHTML(next) {
|
app.use(async (ctx, next) => {
|
||||||
await next;
|
await next();
|
||||||
|
|
||||||
if (!ctx.response.is('html')) return;
|
if (!ctx.response.is('html')) return;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue