http://eggjs.org/zh-cn/basics/objects.html
目录
- 1.1 Application
- 1.2 Context
- 1.3 Request and Response
- 1.4 Helper(扩展)
- 1.5 Config
- 1.6 Logger
egg内置对象
框架内置基础对象:从 Koa 继承而来的 4 个对象(Application
, Context
, Request
, Response
) 以及框架扩展的一些对象(Controller
, Service
, Helper
, Config
, Logger
)
1.1 Application
Application 是全局应用对象,在一个应用中,只会实例化一个,它继承自 Koa.Application
,在它上面我们可以挂载一些全局的方法和对象。我们可以轻松的在插件或者应用中扩展 Application 对象。
几乎所有被框架 Loader 加载的文件(Controller,Service,Schedule 等),都可以 export 一个函数,这个函数会被 Loader 调用,并使用 app 作为参数:
- 在启动脚本中获取
// app.js
module.exports = app => {
app.cache = new Cache();
};
- 在controller中获取
class UserController extends Controller {
async fetch() {
// this.app
this.ctx.body = this.app.cache.get(this.ctx.query.id);
// this.ctx.app 也可以通过ctx对象获取
this.ctx.body = this.ctx.app.cache.get(this.ctx.query.id);
}
}
- 在service中使用
class UserService extends Service {
async fetch() {
this.app; // 得到Appication对象
}
}
1.2 Context
Context 是一个请求级别的对象,继承自 Koa.Context。在每一次收到用户请求时,框架会实例化一个 Context 对象,这个对象封装了这次用户请求的信息,并提供了许多便捷的方法来获取请求参数或者设置响应信息。框架会将所有的 Service 挂载到 Context 实例上,一些插件也会将一些其他的方法和对象挂载到它上面(egg-sequelize 会将所有的 model 挂载在 Context 上)。
- 中间件中使用
async function middleware(ctx, next) {
ctx; // ctx is the instance of Context
}
- service和controller中使用
this.ctx; // 得到Context实例
1.3 Request and Response
都是请求级别的对象,提供了一系列辅助方法获取Http请求与设置Http响应。
通过Context对象获取
ctx.request; // Request instance
ctx.response; // Response instance
1.4 Helper(扩展)
Helper 用来提供一些实用的 utility 函数。它的作用在于我们可以将一些常用的动作抽离在 helper.js 里面成为一个独立的函数,这样可以用 JavaScript 来写复杂的逻辑,避免逻辑分散各处,同时可以更好的编写测试用例。
通过Context对象获取
ctx.helper; // Helper instance
在模板中使用
{{ helper.shtml(value) }}
编写helper:
helper统一放在
app/extend
下
五种扩展方式:Application
, Context
, Request
, Response
, Helper
- Application
框架会把 app/extend/application.js
中定义的对象与 Koa Application 的 prototype 对象进行合并,在应用启动时会基于扩展后的 prototype 生成 app 对象
通过框架扩展的形式自定义helper方法
// app/extend/application.js
module.exports = {
foo(param) {
// this 就是 app 对象,在其中可以调用 app 上的其他方法,或访问属性
},
};
// 使用: app.foo
- Context
// app/extend/context.js
module.exports = {
foo(param) {
// this 就是 ctx 对象,在其中可以调用 ctx 上的其他方法,或访问属性
},
};
// 使用:ctx.foo
- Request
// app/extend/request.js
module.exports = {
get foo() {
return this.get('x-request-foo');
},
};
// 使用: ctx.request.foo
- Response
// app/extend/response.js
module.exports = {
set foo(value) {
this.set('x-response-foo', value);
},
};
// 使用: ctx.response.foo
- Helper
// app/extend/helper.js
module.exports = {
foo(param) {
// this 是 helper 对象,在其中可以调用其他 helper 方法
// this.ctx => context 对象
// this.app => application 对象
},
};
// 使用: ctx.hepler.foo
1.5 Config
通过Application对象获取
app.config; // Config instance
1.6 Logger
分为Application Logger, Context Logger, Controller Logger, Service Logger等,有不同的获取方式
let logger = app.logger; // App Logger
// Context Logger 打印的日志都会在前面带上一些当前请求相关的信息(如 [$userId/$ip/$traceId/${cost}ms $method $url])
logger = ctx.logger; // Context Logger
// Service Logger, Controller Logger本质上就是Context Logger, 不过在打印的时候会额外加上文件路径
logger = this.logger; // 在service或controller中直接获取
// logger 方法
logger.debug();
logger.info();
logger.warn();
logger.error();