Context

Context

new Context()

继承 koa 的 Context

Source:
See:

Members

accept :Boolean

Since:
  • 1.0.0
Source:
See:
  • Request#accept

acceptJSON :Boolean

Since:
  • 1.0.0
Source:
See:

coreLogger :ContextLogger

Logger for frameworks and plugins, wrapping app.coreLogger with context infomation

Since:
  • 1.0.0
Source:

helper :Helper

Get helper instance from Application#Helper

Since:
  • 1.0.0
Source:

ip :string

Since:
  • 1.0.0
Source:
See:

locale

get current request locale

Source:

locals :Object

locals is an object for view, you can use app.locals and ctx.locals to set variables, which will be used as data when view is rendering. The difference between app.locals and ctx.locals is the context level, app.locals is global level, and ctx.locals is request level. when you get ctx.locals, it will merge app.locals.

when you set locals, only object is available

this.locals = {
  a: 1
};
this.locals = {
  b: 1
};
this.locals.c = 1;
console.log(this.locals);
{
  a: 1,
  b: 1,
  c: 1,
};

ctx.locals has cache, it only merges app.locals once in one request.

Source:

logger :ContextLogger

Logger for Application, wrapping app.coreLogger with context infomation

Since:
  • 1.0.0
Source:
Example
```js
this.logger.info('some request data: %j', this.request.body);
this.logger.warn('WARNING!!!!');
```

params :Object

开启 Rest 功能后,将会有 this.params 对象

Source:
Example
##### ctx.params.id {String}

资源 id,如 `GET /api/users/1` => `'1'`

##### ctx.params.ids {Array<String>}

一组资源 id,如 `GET /api/users/1,2,3` => `['1', '2', '3']`

##### ctx.params.fields {Array<String>}

期待返回的资源字段,如 `GET /api/users/1?fields=name,title` => `['name', 'title']`.
即使应用 Controller 实现返回了全部字段,[REST] 处理器会根据 `fields` 筛选只需要的字段。

##### ctx.params.data {Object}

请求数据对象

##### ctx.params.page {Number}

分页码,如 `GET /api/users?page=10` => `10`

##### ctx.params.per_page {Number}

每页资源数目,如 `GET /api/users?per_page=20` => `20`

queries :Array

Since:
  • 1.0.0
Source:
See:

realStatus :Number

Since:
  • 1.0.0
Source:
See:

realStatus :Number

read response real status code.

e.g.: Using 302 status redirect to the global error page instead of show current 500 status page. And access log should save 500 not 302, then the realStatus can help us find out the real status code.

Source:

router :Router

Alias to Application#router

Since:
  • 1.0.0
Source:
Example
```js
this.router.pathFor('post', { id: 12 });
```

starttime :Number

Request start time

Source:

view :ContextView

View instance that is created every request

Source:

Methods

__()

如果开启了 I18n 多语言功能,那么会出现此 API,通过它可以获取到当前请求对应的本地化数据。

详细使用说明,请查看 I18n

  • ctx.__ = function (key, value[, value2, ...]): 类似 util.format 接口
  • ctx.__ = function (key, values): 支持数组下标占位符方式,如
  • __ 的别名是 gettext(key, value)

NOTE: __ 是两个下划线哦!

Source:
Example
```js
ctx.__('{0} {0} {1} {1}'), ['foo', 'bar'])
ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar'])
=>
foo foo bar bar
```
##### Controller 下的使用示例

```js
module.exports = function* () {
  this.body = {
    message: this.__('Welcome back, %s!', this.user.name),
    // 或者使用 gettext,如果觉得 __ 不好看的话
    // message: this.gettext('Welcome back, %s!', this.user.name),
    user: this.user,
  };
};
```

##### View 文件下的使用示例

```html
<li>{{ __('Email') }}: {{ user.email }}</li>
<li>
  {{ __('Hello %s, how are you today?', user.name) }}
</li>
<li>
  {{ __('{0} {0} {1} {1}'), ['foo', 'bar']) }}
</li>
```

##### locale 参数获取途径

优先级从上到下:

- query: `/?locale=en-US`
- cookie: `locale=zh-TW`
- header: `Accept-Language: zh-CN,zh;q=0.5`

curl(url, optionsopt) → {Object}

Shortcut for httpclient.curl

Parameters:
Name Type Description
url String | Object

request url address.

[ options ] Object

options for request.

Source:

getFileStream() → {ReadStream}

get upload file stream

Since:
  • 1.0.0
Source:
Example
```js
const stream = yield this.getFileStream();
// get other fields
console.log(stream.fields);
```

gettext()

ctx.__ 的别名。

Source:
See:

multipart(optionsopt) → {Yieldable}

create multipart.parts instance, to get separated files.

Parameters:
Name Type Description
[ options ] Object

override default multipart configurations

Source:

redirect(url)

实现页面跳转

Parameters:
Name Type Description
url String

需要跳转的地址

Source:
See:
  • Response#redirect