Skip to content

Commit

Permalink
docs: add events on application (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Jan 27, 2018
1 parent 65e0381 commit 06e7710
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/source/en/basics/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ At this chapter, we will introduce some built-in basic objects in the framework,

Application is a global application object, an application only instantiates one Application, it is inherited from [Koa.Application], we can mount some global methods and objects on it. We can easily [extend Application object] (./extend.md#Application) in plugin or application.

### Events

Framework will emits some events when server running, application developers or plugin developers can listen on these events to do some job like logging. As application developers, we can listen on these events in [app start script](./app-start.md).

- `server`: every worker will only emit once during the runtime, after HTTP server started, framework will expose HTTP server instance by this event.
- `error`: if any exception catched by onerror plugin, it will emit an `error` event with the exception instance and current context instance(if have), developers can listen on this event to report or logging.
- `request` and `response`: application will emit `request` and `response` event when receive requests and ended responses, developers can listen on these events to generate some digest log.

```js
// app.js

module.exports = app => {
app.once('server', server => {
// websocket
});
app.on('error', (err, ctx) => {
// report error
});
app.on('request', ctx => {
// log receive request
});
app.on('response', ctx => {
// ctx.starttime is set by framework
const used = Date.now() - ctx.starttime;
// log total cost
});
};
```

### How to Get

Application object can be accessed almost anywhere in application, here are a few commonly used access ways:
Expand Down
29 changes: 29 additions & 0 deletions docs/source/zh-cn/basics/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ title: 框架内置基础对象

Application 是全局应用对象,在一个应用中,只会实例化一个,它继承自 [Koa.Application],在它上面我们可以挂载一些全局的方法和对象。我们可以轻松的在插件或者应用中[扩展 Application 对象](./extend.md#Application)

### 事件

在框架运行时,会在 Application 实例上触发一些事件,应用开发者或者插件开发者可以监听这些事件做一些操作。作为应用开发者,我们一般会在[启动自定义脚本](./app-start.md)中进行监听。

- `server`: 该事件一个 worker 进程只会触发一次,在 HTTP 服务完成启动后,会将 HTTP server 通过这个事件暴露出来给开发者。
- `error`: 运行时有任何的异常被 onerror 插件捕获后,都会触发 `error` 事件,将错误对象和关联的上下文(如果有)暴露给开发者,可以进行自定义的日志记录上报等处理。
- `request``response`: 应用收到请求和响应请求时,分别会触发 `request``response` 事件,并将当前请求上下文暴露出来,开发者可以监听这两个事件来进行日志记录。

```js
// app.js

module.exports = app => {
app.once('server', server => {
// websocket
});
app.on('error', (err, ctx) => {
// report error
});
app.on('request', ctx => {
// log receive request
});
app.on('response', ctx => {
// ctx.starttime is set by framework
const used = Date.now() - ctx.starttime;
// log total cost
});
};
```

### 获取方式

Application 对象几乎可以在编写应用时的任何一个地方获取到,下面介绍几个经常用到的获取方式:
Expand Down

0 comments on commit 06e7710

Please sign in to comment.